


JavaScript uses HTML5's window.postMessage to implement cross-domain communication example_javascript skills
Due to the restrictions of the same-origin policy, cross-domain communication has always been a thorny issue in JavaScript. Of course, there are many solutions:
1. The setting of the document.domain iframe applies to the same main domain but different subdomains;
2. Using iframe and location.hash, the data is directly exposed in the URL, and the data capacity and types are limited
3.Flash LocalConnection, objects can communicate in one SWF file or between multiple SWF files, as long as
are on the same client, across applications, and across domains.
window.name saves data and cross-domain iframe static proxy dynamic transmission scheme makes full use of the feature of window.name that the name does not change because the URL of the page changes.
There are many example codes for various solutions on the Internet, you can search for them yourself.
One of the coolest APIs in html5: Cross Document Messaging. Advanced browsers Internet Explorer 8, chrome, Firefox, Opera and Safari will all support this feature. The implementation of this function is also very simple, mainly including the "message" event for receiving information and the "postMessage" method for sending messages.
The "postMessage" method of sending a message
Send a message to the external window:
otherWindow: refers to the target window, that is, which window to send the message to, which is the window.frames property Members of or the window created by the window.open method
Parameter description:
1.message: is the message to be sent, type is String, Object (not supported by IE8 and 9)
2.targetOrigin: Yes To limit the message receiving range, please use '*' if not limited.
"message" event to receive the message
var onmessage = function (event) {
var data = event.data;
var origin = event.origin;
//do someing
};
if (typeof window.addEventListener != 'undefined') {
window.addEventListener('message', onmessage, false);
} else if (typeof window.attachEvent != 'undefined') {
//for ie
window.attachEvent('onmessage', onmessage);
}
The first parameter of the callback function is received Event objects have three commonly used attributes:
1.data: message
2.origin: message source address
3.source: source DOMWindow object
Of course, postmessage also has some shortcomings:
1.ie8, the data type value passed under ie9 supports string type, but you can use mutual conversion between JSON objects and strings to solve this problem;
2.ie6, ie7 needs to write a compatibility solution , I personally think window.name is more reliable;

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.
