Home Web Front-end JS Tutorial JavaScript uses HTML5's window.postMessage to implement cross-domain communication example_javascript skills

JavaScript uses HTML5's window.postMessage to implement cross-domain communication example_javascript skills

May 16, 2016 pm 04:52 PM
html5 javascript

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:

Copy code The code is as follows:
otherWindow.postMessage(message, targetOrigin);

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

Copy Code The code is as follows:

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;
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1658
14
PHP Tutorial
1257
29
C# Tutorial
1231
24
Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

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

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

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.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

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

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

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

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

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

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

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

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

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

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

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

See all articles