To implement cross-document message transmission, you must first listen to the message event of the window object, and then use the postMessage () method of the window object to send messages to other windows. Next, we will give you a detailed introduction, if you are interested, refer to the following to listen to the message event of the window object.
The Code is as follows:
Window. addEventListener ("message", function (event ){
// Processing program code
}, False );
Use the postMessage () method of the window object to send messages to other windows. The definition of this method is as follows:
The Code is as follows:
Otherwindow. postMessage (message, targetOrigin );
This method uses two parameters: the first parameter is the message text sent, but it can also be any JavaScript Object (converted to text through JSON ); the second parameter is the URL address of the message receiving Object window. You can use the wildcard "*" in the URL string to specify all addresses. However, we recommend that you use an accurate URL address. Otherwindow is a reference to the window object to be sent. You can use window. the open () method returns this object, or the window. the frames array specifies the sequence number (index) or name to return the window object of a single frame.
Example
The Code is as follows:
Cross-document message transmission example Main Document