The window.postMessage() method allows scripts from one document to pass text messages to scripts in another document, regardless of whether they are cross-domain or not. Scripts in one document still cannot call methods and read properties in other documents, but they can communicate securely using this messaging technique.
This technique is called “Cross Document Messaging”, also known as “Inter-Window Messaging” or “Cross Domain Messaging”.
The postMessage() method, which allows limited communication — via asynchronous messaging — between scripts from different origins.
postMessage can be used to solve problems with:
- Data passing of the page and the new window it opens
- Pages with nested iframe messaging
- Message passing between multiple windows
If you want to use postMessage to achieve cross-domain communication and data communication between pages, just remember the postMessage method and message event provided by window.
1. Grammar
otherWindow.postMessage(message, targetOrigin, [transfer]);
otherWindow
A reference to another window, such as the contentWindow property of an iframe, the window object returned by executing window.open, or named or numerically indexed window.frames.
message
data to send. It will be serialized by the structured cloning algorithm, so there is no need to serialize it yourself (some low-version browsers only support strings, so the data sent is best to use JSON.stringify() …
The post postMessage – Cross Domain Messaging first appeared on Lenix Blog .
This article is reprinted from https://blog.p2hp.com/archives/9892
This site is for inclusion only, and the copyright belongs to the original author.