Cross-page message transmission in Html5 and cross-page message in html5

Source: Internet
Author: User

Cross-page message transmission in Html5 and cross-page message in html5

1. To accept messages sent from other windows, you must monitor the message events of window objects.

Window. addEventListener ("message", function () {}, false );

Use the postMessage method of the window object to send messages to other windows,

Otherwindow. postMessage (message, targetOrigin );

Parameter: message is the message text sent, but it can also be any javascript Object;

The second parameter is the URL of the message receiving Object window.

OtherWindow is the reference of the window object to be sent. You can return this object through window. open, or return the window object of a single frame by setting the sequence number or name for the window. frames array.

Create two projects: s1 and s2

S1 and s2 send data to each other:

Index.html in s1:

<! DOCTYPE html> 

1.html in s2

<! DOCTYPE html> 

Result:

Origin attribute: This is the sender meta. The sender Meta is not the same as the url of the website. The original sender only includes the domain name and port number,

In order not to receive messages maliciously sent from other sources, it is best to check the sending source.

Source attribute: obtains the window object for message sending.

 

The message transmission between webpage documents and webpage documents is not only for text messages,

If the Json object stringify method is used to convert the javascript Object into text, use the parse method of the Json object

To restore a javascript Object, any javascript Object can be transmitted between the webpage document and the document, between the port and the port, and between the domain and the domain.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.