


Transmitting messages between pages from different domain names is generally referred to as cross-document messaging, or XDM for short. For example, a page in the www.leemagnum.com domain communicates with a page in the http://blog.csdn.net/lee_magnum domain located in an inline frame. Before the advent of the XDM mechanism, it would have taken a long time to implement this kind of communication without any stress. XDM standardizes this mechanism, allowing us to achieve cross-document communication securely and simply.
The core of XDM is the postMessage() method. For XDM, "another place" refers to the iframe tag included in the current page, or the window that pops up from the current page.
The postMessage() method receives two parameters: a message and a string indicating the domain name from which the message is accepted. The second parameter is very important to ensure secure communication and prevents the browser from sending messages to unsafe places. A small example is as follows
HTML code
JavaScript code
//Get the window in the frame var iframeWin = document.getElementById("iframe").contentWindow; alert(iframeWin) // [object window] //Send a message to the frame iframeWin.postMessage("a message", " http://blog.csdn.net/lee_magnum")
The last line of code in the JavaScript code attempts to send a message to the iframe and formulates Documents in the framework must originate from the "http://blog.csdn.net/lee_magnum" domain. If the source matches, the message is delivered to the iframe, otherwise, postMessage() does nothing. This restriction prevents the position in the window from changing in some situations. If the second parameter passed to postMessage() is "*", it means that the message can be sent to documents from any domain.
When an XDM message is received, the message event of the window object will be triggered. This event is triggered asynchronously, so there may be an event delay from sending the message to receiving the message (triggering the message event of the receiving window). After the message event is triggered, the event object passed to the onmessage handler contains the following three important information.
data: the string data passed in as the first parameter of the postMessage() method
origin: the domain where the document sending the message is located, such as "http://blog.csdn.net /lee_magnum"
Source: The proxy of the window object of the document that sends the message. This proxy object is mainly used to call the postMessage() method in the window that sent the previous message. If the window sending the message comes from the same domain name, then this object is window.
It is very necessary to verify the source of the sending window after receiving the message. Just like specifying a second parameter to the postMessage() method to ensure that the browser does not send the message to an unknown page, checking the source of the message in the onmessage() method handler can ensure that the incoming message comes from a known page. The basic detection mode is as follows
JavaScript code
window.addEventListener('message',function(event){ // Ensure that the message is sent The domain name is a known domain name if(event.origin == "http://blog.csdn.net/lee_magnum"){ //Process the received data processMessage(event.data); //Optional: to the source window Send receipt event.source.postMessage("Received", "http://www.leemagnum.com"); } }, false);
In most cases, Event.Source is just a proxy for the window object, not the actual window object. That is to say, no other information about the window object can be accessed through this event.Source proxy object. The postMessage() method can only be called through the event.Source proxy.
There is another strange thing about XDM. First of all, the first parameter of postMessage() was first implemented as "always a string". But later the definition of this parameter was changed to allow any data structure to be passed in. But not all browsers have implemented this change. Therefore, to be on the safe side, when using the postMessage() method, it is best to only pass strings. If you want to pass in structured data, it is best to call JSON.stringify() on the data to be passed in, pass in the resulting string through the postMessage() method, and then call JSON in the onmessage event handler .parse() method.
Browsers that support XDM include Opera, IE8, Safari 4, Firefox 3.5, Chrome, Webkit for Android and Safari for iOS. For more information about XDM, you can go to the XDM official page for reference. The official page of XDM is http://dev.w3.org/html5/postmsg/
The relevant knowledge of HTML5 actual combat and analysis of cross-document messaging (iframe messaging) is introduced here. For more information about HTML5, please pay attention to the relevant updates of Menglong Station.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment