Created
June 14, 2017 22:04
-
-
Save vijayanandrp/8a86ddb32c051a980cef37d9d5c6951b to your computer and use it in GitHub Desktop.
Passing messages between "iframe" to Parent frame in JavaScript
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
// PARENT - JavaScript (Parent.html) | |
window.addEventListener('message', function (event) { | |
console.log("Hello from -> " + event.data); | |
// Ignores messages from untrusted domains. | |
//if (event.origin != 'URL of Iframe') return; | |
}); | |
// IFRAME - JavaScript (child.html) | |
function keep_alive() { | |
http_request = new XMLHttpRequest(); | |
http_request.open('GET', "Child.html"); | |
http_request.send(null); | |
parent.postMessage("Refreshing Page in child frame", "*"); | |
}; | |
function keep_push() { | |
parent.postMessage("Timeout Message", "*"); | |
}; | |
setInterval(keep_alive, 1000); | |
setTimeout(keep_push, 5000) | |
parent.postMessage("child frame (Initial Page Load)", "*"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment