This article is a mirror article of machine translation, please click here to jump to the original article.

View: 4712|Reply: 2

[HTML/HTML5] HTML5 PostMessage cross-window communication

[Copy link]
Posted on 7/30/2022 10:07:04 PM | | | |
PostMessage is a new cross-origin communication API introduced in html5 that allows you to communicate bidirectionally with the main page and any frame-class page or a page opened by window.open. Its general operation process is as follows:



JS determines whether the current page is an iframe or a popup window
https://www.itsvse.com/thread-10342-1-1.html


postMessage(data,origin) method accepts two parameters:

(1) data: The data to be passed, the HTML5 specification mentions that the parameter can be any basic type of JavaScript or a reproducible object, but not all browsers have done this, some browsers can only handle string parameters, so we need to use the JSON.stringify() method to serialize the object parameters when passing the parameters, and refer to json2.js in the lower version of IE to achieve similar effects.

(2) origin: String parameter, indicate the source of the target window, protocol + host + port number [+URL], the URL will be ignored, so it can not be written, this parameter is for security considerations, the postMessage() method will only pass the message to the specified window, of course, if you want, you can also set the parameter to "*", so that it can be passed to any window, if you want to specify the same origin as the current window, it is set to "/";


Message and receive messages yourself

The code is as follows:



The parent window sends a message to the child window, and the child window receives the message

The code is as follows:




The child window sends a message to the parent window, and the parent window receives the message

The code is as follows:



In addition, a simple listener/client mode is attached to the client for cross-domain messaging at the following address:The hyperlink login is visible.

(End)




Previous:JS determines whether the current page is an iframe or a popup window
Next:Docker modifies and restricts container CPU, memory, and other resources
Posted on 7/30/2022 10:46:07 PM |
Learn to learn...
 Landlord| Posted on 2/18/2023 9:25:31 PM |
Post-robot example of browser window communication
https://www.itsvse.com/thread-10428-1-1.html
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com