html5跨域通訊之postMessage的用法總結(jié)
發(fā)布時(shí)間:2013-11-07 10:32:11 作者:佚名
我要評(píng)論

本文是對(duì)html5跨域通訊之postMessage的用法進(jìn)行了詳細(xì)的總結(jié)介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助
postMessagePortal.html 頁(yè)面代碼
復(fù)制代碼
代碼如下:<!DOCTYPE html>
<title>標(biāo)題</title>
<link rel="stylesheet" href="styles.css">
<link rel="icon" >
<script></p> <p>var targetOrigin = "http://22527.vhost20.boxcdn.cn";</p> <p>var defaultTitle = "Portal";
var notificationTimer = null;</p> <p>function messageHandler(e) {
if (e.origin == targetOrigin) {
notify(e.data);
} else {
// ignore messages from other origins
}
}</p> <p>function sendString(s) {
document.getElementById("widget").contentWindow.postMessage(s, targetOrigin);
}</p> <p>
function notify(message) {
stopBlinking();
blinkTitle(message, defaultTitle);
}</p> <p>function stopBlinking() {
if (notificationTimer !== null) {
clearTimeout(notificationTimer);
}
document.title = defaultTitle;
}</p> <p>function blinkTitle(m1, m2) {
document.title = m1;
notificationTimer = setTimeout(blinkTitle, 1000, m2, m1)
}</p> <p>function sendStatus() {
var statusText = document.getElementById("statusText").value;
sendString(statusText);
}</p> <p>function loadDemo() {
document.getElementById("sendButton").addEventListener("click", sendStatus, true);
document.getElementById("stopButton").addEventListener("click", stopBlinking, true);
sendStatus();
}
window.addEventListener("load", loadDemo, true);
window.addEventListener("message", messageHandler, true);</p> <p></script></p> <p><h1>跨域通訊</h1>
傳遞信息:<input type="text" id="statusText" value="Online">
<button id="sendButton">確定</button>
<iframe id="widget" src="http://22527.vhost20.boxcdn.cn/postMessageWidget.html"></iframe>
<p>
<button id="stopButton">停止標(biāo)題閃爍</button>
</p>
postMessageWidget.html頁(yè)面的代碼
復(fù)制代碼
代碼如下:<!DOCTYPE html>
<title>標(biāo)題</title>
<link rel="stylesheet" href="styles.css">
<script></p> <p>var targetOrigin = "http://www.weixiu0376.cn";</p> <p>// TODO whitelist array</p> <p>function messageHandler(e) {
if (e.origin === "http://www.weixiu0376.cn") {
document.getElementById("status").textContent = e.data;
} else {
// ignore messages from other origins
}
}</p> <p>function sendString(s) {
window.top.postMessage(s, targetOrigin);
}</p> <p>function loadDemo() {
document.getElementById("actionButton").addEventListener("click",
function() {
var messageText = document.getElementById("messageText").value;
sendString(messageText);
}, true);</p> <p>}
window.addEventListener("load", loadDemo, true);
window.addEventListener("message", messageHandler, true);</p> <p></script>
<p>顯示接收信息: <strong id="status"></strong><p>
<div>
<input type="text" id="messageText" value="填寫(xiě)消息內(nèi)容">
<button id="actionButton">發(fā)送消息</button>
</div>
相關(guān)文章
html5 postMessage前端跨域并前端監(jiān)聽(tīng)的方法示例
這篇文章主要介紹了html5 postMessage前端跨域并前端監(jiān)聽(tīng)的方法示例的相關(guān)資料,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-01詳解html5 postMessage解決跨域通信的問(wèn)題
這篇文章主要介紹了詳解html5 postMessage解決跨域通信的問(wèn)題的相關(guān)資料,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-17html5通過(guò)postMessage進(jìn)行跨域通信的方法
這篇文章主要介紹了html5通過(guò)postMessage進(jìn)行跨域通信的方法的相關(guān)資料,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-04- 這篇文章主要介紹了詳解HTML5 window.postMessage與跨域,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-05-11
html5 postMessage解決跨域、跨窗口消息傳遞方案
本篇文章主要介紹了html5 postMessage解決跨域、跨窗口消息傳遞方案,具有一定的參考價(jià)值,有需要的可以了解一下、2016-12-20HTML5中使用postMessage實(shí)現(xiàn)兩個(gè)網(wǎng)頁(yè)間傳遞數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了利用HTML5里的window.postMessage在兩個(gè)網(wǎng)頁(yè)間傳遞數(shù)據(jù)的相關(guān)資料,postMessage API的功能是可以讓你在兩個(gè)瀏覽器窗口或iframe之間傳遞信息數(shù)2016-06-22- window.postMessage經(jīng)常被人們利用來(lái)做跨域數(shù)據(jù)傳遞,下面將為大家來(lái)介紹HTML5中的postMessage API基本使用教程,需要的朋友可以參考下2016-05-20
HTML5中使用postMessage實(shí)現(xiàn)Ajax跨域請(qǐng)求的方法
這篇文章主要介紹了HTML5中使用postMessage實(shí)現(xiàn)Ajax跨域請(qǐng)求的方法的相關(guān)資料,需要的朋友可以參考下2016-04-19Html5 postMessage實(shí)現(xiàn)跨域消息傳遞
這篇文章主要介紹了Html5 postMessage實(shí)現(xiàn)跨域消息傳遞的相關(guān)資料,需要的朋友可以參考下2016-03-11- HTML5提出了一個(gè)新的用來(lái)跨域傳值的方法,即postMessage,這篇文章主要介紹了HTML5的postMessage的使用手冊(cè)的相關(guān)資料,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參2018-12-19