Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vijayanandrp/8a86ddb32c051a980cef37d9d5c6951b to your computer and use it in GitHub Desktop.
Save vijayanandrp/8a86ddb32c051a980cef37d9d5c6951b to your computer and use it in GitHub Desktop.
Passing messages between "iframe" to Parent frame in JavaScript
// 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