HTML5 PostMessage and AddEventListener realize

Source: Internet
Author: User
Tags getmessage

A prerequisite for using PostMessage to implement Cross-domain is a page that invokes another page through <iframe>, as shown in the following example:

In this example, the inner.html is invoked via outter.html

This is outter.html .

<! DOCTYPE html>

This shows inner.html

<! DOCTYPE html>

The example above, if you want to try it,

First, you need to create a new test folder under Tomcat WebApps, and put inner.html and outter.html under the Test folder.

Second, in the computer's Hosts file configuration two domain names 127.0.0.1 test.inner.com and 127.0.0.1 test.outter.com

Third, browser access to the URL is http://test.outter.com:8080/test/outter.html


The relevant knowledge points are as follows:

First, <iframe> must be </iframe>, but not the use of <iframe/>

Second, get the child window object: document.getElementById (""). contentwindow==$ ("") [0].contentwindow==window.frames[0]

Get parent Window object: window.parent

Three, postmessage two parameters, the first parameter is the value to pass across the domain, the second parameter is the scope, including protocol, domain name and port. This is either the corresponding domain name, or the *, which means it is valid for all domains.

The front of the PostMessage defines the window to which this window is being passed a value. This is where the parent window passes a value to the first child window.

The method AddEventListener (type,function,usecapture) can be preceded by elements, document, window, or XMLHttpRequest

Type string, event name, excluding "on", such as Click,mouseover,keydown.

Message//event, OnMessage event occurs every time an update is received.

When it is postmessage, it can be accepted with onmessage, but when it is sendmessage, it cannot be received with OnMessage.

Usecapture for use event capture, generally false//event capture, from top down, and event bubbling opposite

Vi. the difference between SendMessage and PostMessage

PostMessage: It is only responsible for putting messages into message queues, unsure when and whether to process, asynchronously, and return postMessage whether it is properly executed.

SendMessage: Put the message into Message Queuing, wait until the message is processed, return the message Processing return code (DWORD type) before continuing, synchronization, return processing results.

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.