在前一篇文章中,講到瀏覽器同源策略,即阻止不同域的頁面間訪問彼此的方法和屬性,並對解決同源策略跨域問題提出的解決方案進行闡述:子域代理、JSONP 、CORS。本篇將詳細闡述HTML5 window.postMessage,借助postMessage API,文檔間可以以安全可控的方式實現跨域通信,第三方JavaScript代碼也可以與iframe內加載的外部文檔進行通信。
HTML5 window.postMessage APIHTML5 window.postMessage是一個安全的、基於事件的消息API。
postMessage發送消息在需要發送消息的源窗口調用postMessage方法即可發送消息。
源窗口源窗口可以是全局的window對象,也可以是以下類型的窗口:
文檔窗口中的iframe:
var iframe = document.getElementById('my-iframe'); var win = iframe.documentWindow;JavaScript打開的彈窗:
var win = window.open();
當前文檔窗口的父窗口:
var win = window.parent;
打開當前文檔的窗口:
var win = window.opener();
找到源window對像後,即可調用postMessage API向目標窗口發送消息:
``win.postMessage('Hello', 'ttp://jhssdemo.duapp.com/');```postMessage函數接收兩個參數:第一個為將要發送的消息,第二個為源窗口的源。
注:只有當目標窗口的源與postMessage函數中傳入的源參數值匹配時,才能接收到消息。
接收postMessage消息要想接收到之前源窗口通過postMessage發出的消息,只需要在目標窗口註冊message事件並綁定事件監聽函數,就可以在函數參數中獲取消息。
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) ; } };message事件監聽函數接收一個參數,Event對象實例,該對像有三個屬性:
源窗口
<!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>目標窗口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> <div id=txt></div> <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 >回顧通過本篇的學習,了解了使用HTML5的postMessage API在窗口間進行通信,也知道可以藉助其實現跨域通信;現代瀏覽器基本都支持postMessage,而對於一些老式瀏覽器如IE7-等,可以使用一定的替代方案,進行數據通信,如window.name、url查詢字符和hash片段等。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。