Last active
August 29, 2015 13:57
-
-
Save NHQ/9711493 to your computer and use it in GitHub Desktop.
messaging between window and iframe, popups, and new windows (example uses iframe)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// messaging between the parent window and an iframe, popup, or new window (this example uses iframe) | |
var iframe = require('iframe') | |
var $ = function(){ | |
this.addEventListener('message', function(msg){ | |
console.log(msg.data.message) | |
msg.source.postMessage('thanks for the msg', '*') | |
}) | |
} | |
var body = '<script type=text/javascript>(' + ($ + '()') + ')</script>' | |
var frame = iframe({body: body}) | |
setInterval(function(){ | |
frame.iframe.contentWindow.postMessage({message: 'hey'}, '*') | |
}, 1000) | |
window.onmessage = function(msg){ | |
console.log(msg.data) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment