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>