一个窗口更新localStorage,另外一个窗口监听window对象的”storage”事件,来实现通讯。
注:两个页面要同源(URL的协议、域名和端口相同)html
// 本窗口的设值代码 localStorage.setItem('aaa', (Math.random()*10).toString()) // 其余窗口监听storage事件 window.addEventListener("storage", function (e) { console.log(e) console.log(e.newValue) })
全部的WebSocket都监听同一个服务器地址,利用send发送消息,利用onmessage获取消息的变化,不只能窗口,还能跨浏览器,兼容性最佳,只是须要消耗点服务器资源。web
var ws = new WebSocket("ws://localhost:3000/") ws.onopen = function (event) { // 或者把此方法注册到其余事件中,便可与其余服务器通讯 ws.send({now : Date.now()}); // 经过服务器中转消息 }; ws.onmessage = function (event) { // 消费消息 console.log(event.data); }
借助iframe 或 window.open
回顾一下API算法
otherWindow.postMessage(message, targetOrigin, [transfer]);
/* * A窗口的域名是<http://example.com:8080>,如下是A窗口的script标签下的代码: */ var popup = window.open(...popup details...); // 若是弹出框没有被阻止且加载完成 // 这行语句没有发送信息出去,即便假设当前页面没有改变location(由于targetOrigin设置不对) popup.postMessage("The user is 'bob' and the password is 'secret'", "https://secure.example.net"); // 假设当前页面没有改变location,这条语句会成功添加message到发送队列中去(targetOrigin设置对了) popup.postMessage("hello there!", "http://example.org"); function receiveMessage(event) { // 咱们能相信信息的发送者吗? (也许这个发送者和咱们最初打开的不是同一个页面). if (event.origin !== "http://example.org") return; // event.source 是咱们经过window.open打开的弹出页面 popup // event.data 是 popup发送给当前页面的消息 "hi there yourself! the secret response is: rheeeeet!" } window.addEventListener("message", receiveMessage, false); /* * 弹出页 popup 域名是<http://example.org>,如下是script标签中的代码: */ //当A页面postMessage被调用后,这个function被addEventListenner调用 function receiveMessage(event) { // 咱们能信任信息来源吗? if (event.origin !== "http://example.com:8080") return; // event.source 就当前弹出页的来源页面 // event.data 是 "hello there!" // 假设你已经验证了所受到信息的origin (任什么时候候你都应该这样作), 一个很方便的方式就是把enent.source // 做为回信的对象,而且把event.origin做为targetOrigin event.source.postMessage("hi there yourself! the secret response " + "is: rheeeeet!", event.origin); } window.addEventListener("message", receiveMessage, false);
在页面A设置一个使用 setInterval 定时器不断刷新,检查 Cookies 的值是否发生变化,若是变化就进行刷新的操做。浏览器
因为 Cookies 是在同域可读的,因此在页面 B 审核的时候改变 Cookies 的值,页面 A 天然是能够拿到的。安全
这样作确实能够实现我想要的功能,可是这样的方法至关浪费资源。虽然在这个性能过盛的时代,浪费不浪费也感受不出来,可是这种实现方案,确实不够优雅。服务器
HTML5 中的 Web Worker 能够分为两种不一样线程类型,一个是专用线程 Dedicated Worker,一个是共享线程 Shared Worker。cookie
其实就是直接获取对方DOM,适用于两个页面在同一域;能够传递对象数据(对象数据使用 instanceof 作类型判断时有坑);参考 window.open;
例:dom
// 父页面获取子iframe document.getElementById('iframe的id').contentWindow.document // 子iframe获取父页面 window.parent.document
浏览器窗口有window.name属性。这个属性的最大特色是,不管是否同源,只要在同一个窗口里,前一个网页设置了这个属性,后一个网页能够读取它。post
父窗口先打开一个子窗口,载入一个不一样源的网页,该网页将信息写入window.name属性。性能
window.name = data;
接着,子窗口跳回一个与主窗口同域的网址。
window.location.href = 'http://parent.url.com/xxx.html';
而后,主窗口就能够读取子窗口的window.name了。
var data = document.getElementById('iframe的id').contentWindow.name;
这种方法的优势是,window.name容量很大,能够放置很是长的字符串;缺点是必须监听子窗口window.name属性的变化,影响网页性能。