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:
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)
|