HTML5 Learning cross-document transfer messages (vii)

Source: Internet
Author: User

The new standard provides a direct message transfer API between documents. And don't restrict cross-domain messaging! Sending a message using the PostMessage (Data,targeturl) method of the Window object is fine, but to which window object to send a message, use the instance of which window to invoke, paying attention to this detail. The document listens to the message event by default to accept messages: Window.addeventlistener ("message", function (EV) {}); Listening for message events: EV Two important properties: Ev.source point to the source window object that sends the message, Ev.data to get the received message data
<!DOCTYPE HTML><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"/>    <title></title>      <Script>window.onload= function() {document.getElementById ("btn"). onclick= function() {document.getElementById ("F"). Contentwindow.postmessage (document.getElementById ("T"). Value,"*");          };    }; </Script></Head><Body>    <inputID= "T"type= "text" />    <inputID= "BTN"type= "button" />    <iframeID= "F"src= "receivemsg.html" ></iframe></Body></HTML>
<!DOCTYPE HTML><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"/>    <title></title>    <Scripttype= "Text/javascript">Window.onmessage= function(event) {//event.data Data event.origin message Source address Event.source Source Domwindow objectalert (event.data);            alert (Event.origin);        alert (Event.source); }    </Script></Head><Body></Body></HTML>

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.