window.postMessage() 方法可以安全地实现跨域通信。通常,对于两个不同页面的脚本,只有当执行它们的页面位于具有相同的协议(通常为https),端口号(443为https的默认值),以及主机 (两个页面的模数 Document.domain设置为相同的值) 时,这两个脚本才能相互通信。window.postMessage() 方法提供了一种受控机制来规避此限制,只要正确的使用,这种方法就很安全。

一、安装live-server

要想实现跨窗口通信,必须要在服务器上运行,直接用浏览器打开HTML文件只能处理单个文件,窗口之间无法通信。

npm install -g live-server

使用命令live-server进行启动。

安装live-server,在任意位置启动服务器。

在服务器中启动之后,会看见文档中多了一个script标签。这段代码是由live-server插入的。当运行在后台的live-server检测到文件变化,就会通过websockt向网页发送“reload”消息,从而可以实现浏览器中的网页总是实时的响应文件的变更。

	// <![CDATA[  <-- For SVG support
if ('WebSocket' in window) {
(function() {
function refreshCSS() {
var sheets = [].slice.call(document.getElementsByTagName("link"));
var head = document.getElementsByTagName("head")[0];
for (var i = 0; i < sheets.length; ++i) {
var elem = sheets[i];
head.removeChild(elem);
var rel = elem.rel;
if (elem.href && typeof rel != "string" || rel.length == 0 || rel.toLowerCase() == "stylesheet") {
var url = elem.href.replace(/(&|\?)_cacheOverride=\d+/, '');
elem.href = url + (url.indexOf('?') >= 0 ? '&' : '?') + '_cacheOverride=' + (new Date().valueOf());
}
head.appendChild(elem);
}
}
var protocol = window.location.protocol === 'http:' ? 'ws://' : 'wss://';
var address = protocol + window.location.host + window.location.pathname + '/ws';
var socket = new WebSocket(address);
socket.onmessage = function(msg) {
if (msg.data == 'reload') window.location.reload();
else if (msg.data == 'refreshcss') refreshCSS();
};
console.log('Live reload enabled.');
})();
}
// ]]>

二、基础知识

MessageEvent有以下几个属性:

  • data:从其他window中传递过来的对象
  • origin:调用 postMessage 时消息发送方窗口的 origin
  • source:对发送消息的窗口对象的引用; 您可以使用此来在具有不同origin的两个窗口之间建立双向通信。

在发送数据窗口执行:otherWindow.postMessage(msg,origin)

  • otherWindow:表示接受数据的窗口的window对象,包括iframe的子窗口和通过window.open打开的新窗口。
  • msg表示要发送的数据,包扩字符串和对象(ie9以下不支持,可以利用字符串和json互换)。
  • origin表示接收的域名。

三、最简单的一个demo

父窗口打开一个子窗口,然后询问子窗口:“吃饭了吗”,子窗口回复父窗口:“吃了”

father.html

<html>

<body>

</body>
<script>
window.addEventListener("message", function(e) {
document.querySelector("body").appendChild(document.createTextNode('son say: ' + e.data))
})
var son = window.open("son.html")
son.onload = function() {//必须得要等到儿子加载完成才可以说话
son.postMessage("吃饭了吗", location.href)
}
</script> </html>

son.html

<html>

<body>

</body>
<script>
window.addEventListener("message", function() {
console.log(event)
document.querySelector("body").appendChild(document.createTextNode("father say: " + event.data))
event.source.postMessage("吃了", event.origin)
})
</script> </html>

四、一个网页聊天系统

father.html

<html>

<head>
<style>
textarea,
input {
width: 80%;
font-size: 20px;
font-family: "Consolas";
} textarea {
height: 80%;
} input {
height: 10%;
}
</style>
</head> <body>
<div style="text-align:center">
<textarea readonly></textarea>
<input type="text" style="margin-top:10px" onkeydown="keydown()">
</div>
</body>
<script>
function $(sel) {
return document.querySelector(sel)
}
window.addEventListener("message", function() {
$("textarea").value += "\nson say: " + event.data
})
var son = window.open("myson.html") function keydown() { //这里不需要传递参数,直接使用event就可以
if (event.keyCode == 13) {
son.postMessage($("input").value, location.href)
$("textarea").value += "\n我说:" + $("input").value
$("input").value = ""
event.preventDefault
}
}
</script> </html>

myson.html

<html>

<head>
<style>
textarea,
input {
width: 80%;
font-size: 20px;
font-family: "Consolas";
} textarea {
height: 80%;
} input {
height: 10%;
}
</style>
</head> <body> <div style="text-align:center">
<textarea readonly></textarea>
<input type="text" style="margin-top:10px" onkeydown="keydown()">
</div>
</body>
<script>
var father = null
window.addEventListener("message", function() {
$("textarea").value += "\nfather say: " + event.data
if (father == null) {
father = {
source: event.source,
origin: event.origin
}
}
}) function $(sel) {
return document.querySelector(sel)
} function keydown() { //这里不需要传递参数,直接使用event就可以
if (event.keyCode == 13) {
father.source.postMessage($("input").value, location.href)
$("textarea").value += "\n我说:" + $("input").value
$("input").value = ""
event.preventDefault
}
}
</script> </html>

五、最后一个demo

<html>

<head>
<meta charset="UTF-8">
</head> <body>
<input type="button" value="Open Window" onclick="openWin()" />
</body>
<script>
window.addEventListener("message", function(evt) {
var ele = document.createElement("pre")
ele.innerText = "son say:" + JSON.stringify(evt.data)
document.querySelector("body").appendChild(ele)
}) var popupwin = window.open("son.html");
//onload只能执行一次,也就是如果子窗口有onload事件,可能会覆盖。
popupwin.onload = function(e) {
var params = "天下大势为我所控"
var origin = location.href
popupwin.postMessage(params, origin);
}
popupwin.onunload = function(e) {
var ele = document.createElement("h1")
ele.innerText = "儿子最后说:" + popupwin.returnValue
document.querySelector("body").appendChild(ele)
}
</script> </html>

son.html

<html>

<head>
<title>popup window</title>
</head> <body>
<button onclick="closeWin()">点我返回</button>
<div id="show"></div>
</body>
<script>
function closeWin() {
window.returnValue = "这是返回值";
window.close();
}
//HTML DOM fully loaded, and fired window.onload later.
document.onreadystatechange = function() {
if (document.readyState === 'complete') {
window.addEventListener('message', function(event) {
document.querySelector("#show").appendChild(document.createTextNode("father say:" + e.data))
event.source.postMessage("what's the fuck", event.origin)
});
}
};
</script> </html>

六、安全问题

如果您不希望从其他网站接收message,请不要为message事件添加任何事件侦听器。

如果您确实希望从其他网站接收message,请始终使用origin和source属性验证发件人的身份。

当您使用postMessage将数据发送到其他窗口时,始终指定精确的目标origin,而不是*。

参考资料

https://developer.mozilla.org/zh-CN/docs/Web/API/Window/postMessage

window.postMessage实现网页间通信的更多相关文章

  1. JavaScript 跨域:window.postMessage 实现跨域通信

    JavaScript 跨域方式实现方式有很多,之前,一篇文章中提到了 JSONP 形式实现跨域.本文将介绍 HTML5 新增的 api 实现跨域:window.postMessage . 1 othe ...

  2. 利用HTML5的window.postMessage实现跨域通信

    详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp77   HTML5的window.postMessage简述 postM ...

  3. window.postMessage跨文档通信

    window.postMessage 1.浏览器兼容情况:IE8+.chrome.firefox等较新浏览器都至此. 2.使用方法: a.otherWindow.postMessage( messag ...

  4. 使用window.postMessage实现跨域通信

    JavaScript由于同源策略的限制,跨域通信一直是棘手的问题.当然解决方案也有很多: document.domain+iframe的设置,应用于主域相同而子域不同: 利用iframe和locati ...

  5. 用HTML5里的window.postMessage在两个网页间传递数据

    说明 window.postMessage()方法可以安全地实现Window对象之间的跨域通信.例如,在一个页面和它生成的弹出窗口之间,或者是页面和嵌入其中的iframe之间. 通常情况下,不同页面上 ...

  6. 跨域通信--Window.postMessage()

    一.跨源通信概述 源:协议.端口号(https默认值433).主机域名(document.domain) 作用:向目标窗口派发MessageEvent消息(四个属性) 兼容参考 MessageEven ...

  7. window.postMessage 跨窗口,跨iframe javascript 通信

    同源通信 执行它们的页面位于具有相同的协议(http/https),端口(80/443),主机(通常为域名) 时,这两个脚本才能相互通信 大多数情况下,网站就是内部的域名,所以是同源通信,可以相互访问 ...

  8. HTML5之worker开启JS多线程模式及window.postMessage跨域

    worker概述 worker基本使用 window下的postMessage worker多线程的应用 一.worker概述 web worker实际上是开启js异步执行的一种方式.在html5之前 ...

  9. 系统间通信(9)——通信管理与RMI 下篇

    接上文<架构设计:系统间通信(8)--通信管理与RMI 上篇>.之前说过,JDK中的RMI框架在JDK1.1.JDK1.2.JDK1.5.JDK1.6+几个版本中做了较大的调整.以下我们讨 ...

随机推荐

  1. MIR Flickr 1M 图像数据集(点击即可下载)

    Index of /mirflickr/mirflickr1m Name Last modified Size Description Parent Directory   -   exif.zip ...

  2. 【BZOJ】【1178】【APIO2009】convention会议中心

    贪心 如果不考虑字典序的话,直接按右端点排序,能选就选,就可以算出ans…… 但是要算一个字典序最小的解就比较蛋疼了= = Orz了zyf的题解 就是按字典序从小到大依次枚举,在不改变答案的情况下,能 ...

  3. Servlet监听器统计在线人数

    监听器的作用是监听Web容器的有效事件,它由Servlet容器管理,利用Listener接口监听某个执行程序,并根据该程序的需求做出适应的响应. 例1 应用Servlet监听器统计在线人数. (1)创 ...

  4. QT学习:c++解析html相关

    原来我做爬虫的时候,对页面进行解析的时候总是用很简单粗暴的方法,直接找规律.后来在网上看到了gumbo,尝试了一下,发现确实很好用,所以向大家推荐一下. 以下转自:http://blog.csdn.n ...

  5. 第十章 Redis持久化--RDB+AOF

    注:本文主要参考自<Redis设计与实现> 1.Redis两种持久化方式 RDB 执行机制:快照,直接将databases中的key-value的二进制形式存储在了rdb文件中 优点:性能 ...

  6. 利用CSS、JavaScript及Ajax实现图片预加载的三大方法及优缺点分析

    预加载图片是提高用户体验的一个很好方法.图片预先加载到浏览器中,访问者便可顺利地在你的网站上冲浪,并享受到极快的加载速度.这对图片画廊及图片占据很大比例的网站来说十分有利,它保证了图片快速.无缝地发布 ...

  7. Callable、Future&阻塞队列&阻塞栈

    Callable.Future   简单应用 在Java5之前,线程是没有返回值的,常常为了“有”返回值,破费周折,而且代码很不好写.或者干脆绕过这道坎,走别的路了.现在Java终于有可返回值的任务( ...

  8. PHP 解决版本问题:"Assigning the return value of new by reference is deprecated"

    问题描述:     在最近使用ECSHOP v273帮客户建立了一个商城系统,商城搭建一切ok但在使用中后台发现了一个500错误     在服务器上访问该地址发现了错误信息:"Assigni ...

  9. 齐次坐标(Homogeneous Coordinates)

    原文:http://blog.163.com/m_note/blog/static/208197045201272341230195/ 齐次坐标(Homogeneous Coordinates) 问题 ...

  10. 安装--->Tomcat监控工具Probe

    1.Porbe介绍 psi-probe用于对Tomcat进行监控,比tomcat的manager强大很多. 2.下载 probe-2.3.3.zip  或者  probe.war 3.将下载好的war ...