html5跨文档消息传递
2011-06-16 11:51
351 查看
基于js的同源策略使文档不能跨域访问,为此html5提供了跨文档消息传递功能,下贴出示例...
http://localhost:4933站页面代码
<!DOCTYPE html>
<head>
<title></title>
<script type="text/javascript">
window.addEventListener("message", function(ev) {
ev.origin == "http://www.woyun.com" && alert(ev.origin + "说:" + ev.data);
}, false);
function say() {
document.getElementById("myifr").contentWindow.postMessage("Hello", "http://www.woyun.com");
}
</script>
</head>
<body>
<iframe src="http://www.woyun.com/mytest.htm" onload="say()" id="myifr"></iframe>
</body>
</html>
http://www.woyun.com/站页面代码
<!DOCTYPE html>
<head>
<title></title>
<script>
window.addEventListener("message", function(ev) {
if (ev.origin == "http://localhost:4933") {
document.body.innerHTML = ev.origin + "说:" + ev.data;
ev.source.postMessage(ev.data.split("").reverse().join(""), ev.origin);
}
}, false);
</script>
</head>
<body>
</body>
</html>
运行效果截图...
使用跨文档消息传递首先监控窗口message事件
事件对象包括的属性
data:传递的内容,可以是字符串或对象
origin:发送消息的窗口的协议、域名和端口号
source:发送消息的窗口对象
使用窗口对象的postMessage发送消息,postMessage接受俩个参数,传递的内容和接受页面的origin...
http://localhost:4933站页面代码
<!DOCTYPE html>
<head>
<title></title>
<script type="text/javascript">
window.addEventListener("message", function(ev) {
ev.origin == "http://www.woyun.com" && alert(ev.origin + "说:" + ev.data);
}, false);
function say() {
document.getElementById("myifr").contentWindow.postMessage("Hello", "http://www.woyun.com");
}
</script>
</head>
<body>
<iframe src="http://www.woyun.com/mytest.htm" onload="say()" id="myifr"></iframe>
</body>
</html>
http://www.woyun.com/站页面代码
<!DOCTYPE html>
<head>
<title></title>
<script>
window.addEventListener("message", function(ev) {
if (ev.origin == "http://localhost:4933") {
document.body.innerHTML = ev.origin + "说:" + ev.data;
ev.source.postMessage(ev.data.split("").reverse().join(""), ev.origin);
}
}, false);
</script>
</head>
<body>
</body>
</html>
运行效果截图...
使用跨文档消息传递首先监控窗口message事件
事件对象包括的属性
data:传递的内容,可以是字符串或对象
origin:发送消息的窗口的协议、域名和端口号
source:发送消息的窗口对象
使用窗口对象的postMessage发送消息,postMessage接受俩个参数,传递的内容和接受页面的origin...
相关文章推荐
- JavaScript:HTML5跨文档消息传递
- HTML5实战与剖析之跨文档消息传递(iframe传递信息)
- HTML5跨文档消息通信之postMessage API的探索案例
- 跨文档消息传递应用
- 页面通信方式——跨文档消息传递XDM
- postMessage 跨文档消息传递
- VC++ 绘图控制(对话框和单文档消息传递)
- HTML5实战与剖析之跨文档消息传递(iframe传递信息)
- MFC消息传递(单文档与对话框)
- 单文档菜单消息的传递
- 跨 html 消息传递
- HTML5中的跨文档消息传递
- HTML5 脚本编程之:跨文档消息传递&历史状态管理
- H5 postMessage跨文档消息传递
- HTML5中的跨文档消息传递
- 官方:FlashVars 将变量从 HTML 中传递到 Flash 文档,请执行以下操作
- MFC消息传递
- HTML 帮助(*.chm) 文档的小问题
- MINXI笔记之消息传递
- 【洛谷2018】消息传递