Last active
March 7, 2017 06:40
-
-
Save bacanu/a17573714ed9ea4a476607724fd467f9 to your computer and use it in GitHub Desktop.
Pass messages to iframe. @see https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage
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
| <!doctype html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" | |
| content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> | |
| <meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
| <title>Document</title> | |
| </head> | |
| <body> | |
| <h1>Hello World</h1> | |
| <script> | |
| window.addEventListener("message", receiveMessage, false); | |
| function receiveMessage(event) | |
| { | |
| console.log(event); | |
| } | |
| </script> | |
| </body> | |
| </html> |
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
| <!doctype html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" | |
| content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> | |
| <meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
| <title>Document</title> | |
| </head> | |
| <body> | |
| <iframe src="iframe.html" id="theiframe" frameborder="0"></iframe> | |
| <script> | |
| var iframe = document.getElementById("theiframe").contentWindow; | |
| iframe.postMessage("hello", "*"); // doesnt work, should only send message after iframe is loaded | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment