Skip to content

Instantly share code, notes, and snippets.

@rwaddin
Created April 5, 2021 06:22
Show Gist options
  • Select an option

  • Save rwaddin/2547fd3bb8d25e7518376eca0ea749b7 to your computer and use it in GitHub Desktop.

Select an option

Save rwaddin/2547fd3bb8d25e7518376eca0ea749b7 to your computer and use it in GitHub Desktop.
Communication parent and child iframe
window.addEventListener('message', function(e) {
// Get the sent data
const data = e.data;
// If you encode the message in JSON before sending them,
// then decode here
// const decoded = JSON.parse(data);
});
// how to send data
// 1. from child iframe to parent
const message = JSON.stringify({
message: 'Hello from iframe',
date: Date.now(),
});
window.parent.postMessage(message, '*');
// 2. from parent to child
// this sample with jquery you can use native js with modify
$("iframe").contentWindow.postMessage(message, '*');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment