Created
April 5, 2017 12:43
-
-
Save TianyiLi/e7839116f77a7ba4eed9ebea49298a83 to your computer and use it in GitHub Desktop.
iframe cross domain
This file contains hidden or 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
// from parent javascript would be | |
window.addEventListener('message', function(event){ | |
// event handle | |
console.log(event) | |
}) | |
// use button click to send event 'test' could be anything | |
function send(){ | |
let iframe = window.frames[0]; | |
iframe.postMessage('test', document.querySelector('iframe').src); | |
} | |
// from child iframe usage | |
window.addEventListener('message', function(event){ | |
// Can recording the event.source and do postMessage to the parent window | |
console.log(event.origin); | |
event.source.postMessage('test','http://localhost:8080'); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment