国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > HTML > 正文

html5跨域通訊之postMessage的用法總結

2020-03-24 16:31:03
字體:
來源:轉載
供稿:網友
postMessagePortal.html 頁面代碼

復制代碼代碼如下:
!DOCTYPE html
title 標題 /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" 停止標題閃爍 /button
/p

postMessageWidget.html頁面的代碼

復制代碼代碼如下:
!DOCTYPE html
title 標題 /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="填寫消息內容"
button id="actionButton" 發送消息 /button
/div
html教程

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 通榆县| 贡觉县| 宁远县| 四会市| 永仁县| 临湘市| 遂溪县| 松溪县| 武宣县| 海兴县| 万荣县| 克拉玛依市| 龙川县| 德保县| 墨江| 沈阳市| 信阳市| 长寿区| 谷城县| 陆良县| 油尖旺区| 东丽区| 阜平县| 昭苏县| 潜江市| 黑河市| 武安市| 云安县| 莫力| 莱芜市| 思茅市| 建瓯市| 彰化县| 浪卡子县| 宜良县| 华池县| 常德市| 馆陶县| 砚山县| 长治市| 贵港市|