This time I will bring you H5 window.postMessage and cross-domain, window.postMessage and cross-domain NotesWhat are the practical cases below, let’s take a look.
In the previous article, we talked about the browser's same-origin policy, which prevents pages from different domains from accessing each other's methods and attributes, and elaborated on the solutions to solve the cross-domain problem of the same-origin policy. : Subdomain proxy, JSONP, CORS. This article will elaborate on HTML5 window.postMessage. With the postMessage API, cross-domain communication can be achieved between documents in a safe and controllable manner. Third-party JavaScript code can also communicate with external documents loaded in iframes.
HTML5 window.postMessage API
HTML5 window.postMessage is a secure, event-based messaging API.
postMessageSend message
Call the postMessage method in the source window where the message needs to be sent to send the message.
Source window
The source window can be the global window object, or it can be the following type of window:
Document iframe in the window:
var iframe = document.getElementById('my-iframe'); var win = iframe.documentWindow;
Pop-up window opened by JavaScript:
var win = window.open();
Parent window of the current document window:
var win = window.parent;
Open the window of the current document:
var win = window.opener();
After finding the source window object, you can call the postMessage API to send a message to the target window:
``win.postMessage('Hello', 'ttp://jhssdemo.duapp.com/');"
The postMessage function receives two parameters: the first is the message to be sent, and the second is the source of the source window. .
Note: The message can only be received when the source of the target window matches the source parameter value passed in the postMessage function.
Receive postMessage message
To receive the message previously sent by the source window through postMessage, you only need to register the message event in the target window and bind the event listening function. The message can be obtained in Function parameters.
window.onload = function() { var text = document.getElementById('txt'); function receiveMsg(e) { console.log("Got a message!"); console.log("nMessage: " + e.data); console.log("nOrigin: " + e.origin); // console.log("Source: " + e.source); text.innerHTML = "Got a message!<br>" + "Message: " + e.data + "<br>Origin: " + e.origin; } if (window.addEventListener) { //为窗口注册message事件,并绑定监听函数 window.addEventListener('message', receiveMsg, false); }else { window.attachEvent('message', receiveMsg); } };
The message event listening function receives a parameter, an Event object instance, which has three attributes:
data The specific message sent
origin Send message source
source Window object reference of the send message window
Description
You can set the second parameter of the postMessage function to * to skip checking the source of the message when sending a cross-domain message.
postMessage can only send string information.
Example
Source window
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Html5 postMessage</title> <style> #otherWin { width: 600px; height: 400px; background-color: #cccccc; } </style> </head> <body> <button id="btn">open</button> <button id="send">send</button> <!-- 通过 iframe 嵌入子页面(接收消息目标窗口) --> <iframe src="http://jhssdemo.duapp.com/demo/h5_postmessage/win.html" id="otherWin"></iframe> <br/><br/> <input type="text" id="message"><input type="button" value="Send to child.com" id="sendMessage" /> <script> window.onload = function() { var btn = document.getElementById('btn'); var btn_send = document.getElementById('send'); var sendBtn = document.getElementById('sendMessage'); var win; btn.onclick = function() { //通过window.open打开接收消息目标窗口 win = window.open('http://jhssdemo.duapp.com/demo/h5_postmessage/win.html', 'popUp'); } btn_send.onclick = function() { // 通过 postMessage 向子窗口发送数据 win.postMessage('Hello', 'http://jhssdemo.duapp.com/'); } function sendIt(e){ // 通过 postMessage 向子窗口发送数据 document.getElementById("otherWin").contentWindow .postMessage( document.getElementById("message").value, "http://jhssdemo.duapp.com/"); } sendBtn.onclick = function(e) { sendIt(e); }; }; </script> </body> </html>
Target window win.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Html5 postMessage</title> <style> #txt { width: 500px; height: 300px; background-color: #cccccc; } </style> </head> <body> <h1>The New Window</h1> <p id="txt"></p> <script> window.onload = function() { var text = document.getElementById('txt'); //监听函数,接收一个参数--Event事件对象 function receiveMsg(e) { console.log("Got a message!"); console.log("nMessage: " + e.data); console.log("nOrigin: " + e.origin); text.innerHTML = "Got a message!<br>" + "Message: " + e.data + "<br>Origin: " + e.origin; } if (window.addEventListener) { //为window注册message事件并绑定监听函数 window.addEventListener('message', receiveMsg, false); }else { window.attachEvent('message', receiveMsg); } }; </script> </body> </html>
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
detailed explanation of phonegap obtaining location information
detailed explanation of phonegap creating contacts
The above is the detailed content of H5 window.postMessage and cross-domain. For more information, please follow other related articles on the PHP Chinese website!