Created
April 5, 2021 06:22
-
-
Save rwaddin/2547fd3bb8d25e7518376eca0ea749b7 to your computer and use it in GitHub Desktop.
Communication parent and child iframe
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
| 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); | |
| }); |
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
| // 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