html5跨域通讯之postMessage的用法总结

所属分类: 网页制作 / html5 阅读数: 1017
收藏 0 赞 0 分享

postMessagePortal.html 页面代码

复制代码
代码如下:

<!DOCTYPE html>
<title>标题</title>
<link rel="stylesheet" href="styles.css">
<link rel="icon" href="http://apress.com/favicon.ico">
<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>

更多精彩内容其他人还在看

HTML5的标签的代码的简单介绍 HTML5标签的简介

本文主要介绍了HTML5的标签.不同于以前的标签.更简单.更方便
收藏 0 赞 0 分享

关于HTML5你必须知道的28个新特性,新技巧以及新技术

HTML5有很多的新功能.新代码.非常不错.现在总结一下.仅供参考
收藏 0 赞 0 分享

只要五步 就可以用HTML5/CSS3快速制作便签贴特效(图)

用CSS,HTML5打造一个漂亮的标签效果
收藏 0 赞 0 分享

网易微博Web App用HTML5开发的过程介绍

本文介绍了网易微博用HTML5开发的全过程
收藏 0 赞 0 分享

HTML5 对各个标签的定义与规定:body的介绍

本文主要介绍body标签
收藏 0 赞 0 分享

关于HTML5的安全问题开发人员需要牢记的

HTML5中的安全问题也要注意的
收藏 0 赞 0 分享

关于HTML5的22个初级技巧(图文教程)

HTML5来了.让我们看一下有什么技巧
收藏 0 赞 0 分享

开发人员所需要知道的HTML5性能分析面面观

以下这篇文章是由一位名为张黎明的IT技术人员所写,其发表于InfoQ的网页上。这次他在全文里面从9个不同的方面分析HTML5的性能,还是很值得相应的开发人员阅读的。
收藏 0 赞 0 分享

HTML5安全介绍之内容安全策略(CSP)简介

前言:HTML5出现后,网络安全更加受到广泛的关注。Web对于网络安全有哪些改进?我们如何来面对越来越危险的网络欺诈和攻击?下面的文章谈到了W3C对于这个问题的最新解决方案。未来有机会的话,我会针对XSS、P3P、同源策略、CORS(跨域资源共享)和CSP进行关于HTML5内容
收藏 0 赞 0 分享

input元素的url类型和email类型简介

在过去我们制作网页输入框,会用到不少JS验证,如今有了HTML5写这种效果已经没有那么麻烦了,下面我来给大家介绍两种HTML5的input的新增加的类型应用。
收藏 0 赞 0 分享
查看更多