Skip to content

Instantly share code, notes, and snippets.

@mattlundstrom
Created March 28, 2016 23:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattlundstrom/db1438ae35323e20504d to your computer and use it in GitHub Desktop.
Save mattlundstrom/db1438ae35323e20504d to your computer and use it in GitHub Desktop.
// FROM http://stackoverflow.com/questions/25098021/securityerror-blocked-a-frame-with-origin-from-accessing-a-cross-origin-frame
In your main page:
var frame = document.getElementById('your-frame-id');
frame.contentWindow.postMessage(/*any variable or object here*/, '*');
In your <iframe> (contained in the main page):
window.addEventListener('message', function(event) {
// IMPORTANT: Check the origin of the data!
if (~event.origin.indexOf('http://yoursite.com')) {
// The data has been sent from your site
// The data sent with postMessage is stored in event.data
console.log(event.data);
} else {
// The data hasn't been sent from your site!
// Be careful! Do not use it.
return;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment