這篇文章主要為大家展示了“H5中window.postMessage與跨域的示例分析”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“H5中window.postMessage與跨域的示例分析”這篇文章吧。
HTML5 window.postMessage API
HTML5 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對象實例,該對象有三個屬性:
data 發送的具體消息
origin 發送消息源
source 發送消息窗口的window對象引用
說明
可以將postMessage函數第二個參數設為*,在發送跨域消息時會跳過對發送消息的源的檢查。
postMessage只能發送字符串信息。
實例
源窗口
<!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/h6_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/h6_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>
<h2>The New Window</h2>
<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>以上是“H5中window.postMessage與跨域的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。