One day, the leader put forward a requirement to embed an iframe in the main page and send messages between the parent and child pages in both directions. Then I proposed a solution: postMessage Knowledge theory preparation: The postMessage method allows scripts from different sources to communicate asynchronously in a limited manner, and can achieve cross-text document, multi-window, and cross-domain message delivery. grammar: otherWindow.postMessage(message, targetOrigin, [transfer]);
When the postMessage method is called, a MessageEvent message is dispatched to the target window after all page scripts have been executed. The MessageEvent message has four properties that need to be noted:
The operation process is as follows: 1. Prepare two pages
main.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>iframe+postMessage cross-domain communication main page</title> </head> <body> <h1>Main Page</h1> <iframe id="child" src="http://b.com/iframepage.html"></iframe> <div> <h2>Main page receiving message area</h2> <span id="message"></span> </div> </body> </html> iframepage.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>iframe+postMessage cross-domain communication subpage</title> </head> <body> <h2>Subpages</h2> <div> <h3>Receive message area</h3> <span id="message"></span> </div> </body> </html> 2. Parent sends message to child main.html <script> window.onload = function(){ document.getElementById('child') .contentWindow.postMessage("Main page message", "http://b.com/iframepage.html") } </script> Notice: The message must be sent after the page is loaded, otherwise an error will be reported because the iframe has not been loaded.
Subpage receives message: iframepage.html <script> window.addEventListener('message',function(event){ console.log(event); document.getElementById('message').innerHTML = "Received" + event.origin + "Message: " + event.data; }, false); </script> At this point, you can see that the page is printed in the iframe's subpage.
And the console prints the MessageEvent object. 3. The child sends a message to the parent The child page replies to the parent page after receiving the message iframepage.html <script> window.addEventListener('message',function(event){ console.log(event); document.getElementById('message').innerHTML = "Received" + event.origin + "Message: " + event.data; top.postMessage("Subpage message received", 'http://a.com/main.html') }, false); </script> The parent page receives the message and displays: window.addEventListener('message', function(event){ document.getElementById('message').innerHTML = "Received" + event.origin + "Message: " + event.data; }, false); 4. Complete code main.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>iframe+postMessage cross-domain communication main page</title> </head> <body> <h1>Main Page</h1> <iframe id="child" src="http://b.com/iframepage.html"></iframe> <div> <h2>Main page receiving message area</h2> <span id="message"></span> </div> </body> <script> window.onload = function(){ document.getElementById('child') .contentWindow.postMessage("Main page message", "http://b.com/iframepage.html") } window.addEventListener('message', function(event){ document.getElementById('message').innerHTML = "Received" + event.origin + "Message: " + event.data; }, false); </script> </html> iframepage.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>iframe+postMessage cross-domain communication subpage</title> </head> <body> <h2>Subpages</h2> <div> <h3>Receive message area</h3> <span id="message"></span> </div> </body> <script> window.addEventListener('message',function(event){ if(window.parent !== event.source){return} console.log(event); document.getElementById('message').innerHTML = "Received" + event.origin + "Message: " + event.data; top.postMessage("Subpage message received", 'http://a.com/main.html') }, false); </script> </html> This is the end of this article about the implementation example of html parent-child page iframe two-way messaging. For more relevant html parent-child page iframe two-way messaging content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! |
<<: Introduction to using Unicode characters in web pages (&#,\u, etc.)
>>: CSS achieves colorful and smart shadow effects
Using Navicat directly to connect via IP will rep...
This article example shares the specific code of ...
Table of contents 1. What is 2. Use Numeric Enume...
Shtml and asp are similar. In files named shtml, s...
Problem Record Today I was going to complete a sm...
Preface In this article, we will use Docker to bu...
I found a lot of websites that use drop-down or sl...
1. addtime() Add the specified number of seconds ...
This article shares the specific code of the WeCh...
Without further ado, let’s get straight to the co...
Table of contents Preface About webSocket operati...
1. Stop the mysqld.exe process first 2. Open cmd ...
When the system encounters various IO bottlenecks...
Code first, then text Copy code The code is as fol...
Table of contents Preface question Online solutio...