Skip to content

Instantly share code, notes, and snippets.

@NHQ
Last active August 29, 2015 13:57
Show Gist options
  • Save NHQ/9711493 to your computer and use it in GitHub Desktop.
Save NHQ/9711493 to your computer and use it in GitHub Desktop.
messaging between window and iframe, popups, and new windows (example uses iframe)
// 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