本文共 4557 字,大约阅读时间需要 15 分钟。
一、同域下页面之间的通信
该条件下实现的方式比较多,先列举几种典型的方法,然后举例说明其中几种。
1、iframe标签可以嵌套另一个标签,并且可以通过js去访问被包含的页面的window对象,从而操作该页面下documentElement; 2、使用H5中的webstorage来实现,利用localeStorage和sessionStorage存储进行数据通信; 3、使用cookie,读取相同域名下的cookie值来进行通信; 4、Window.name通信。(有专门的文章进行讲解) 例子1:有一个源窗口A和一个目标窗口B,B是在A中以超链接的形式打开的,在两个窗口同时存在的场景下,如何保证这两个窗口间的持续性通讯。 其中窗口A:var message = '';document.querySelector("#boy").onclick = function() { message = '我是男生,帅气的男生!';};document.querySelector("#girl").onclick = function() { message = '我是女生,漂亮的女生!';};window.addEventListener('message', function(e) { var interval; // 检测来源 if (e.origin == 'http://www.zhangxinxu.com') switch (e.data) { case 'ready': // e.source 为发送的 window 对象 interval = setTimeout(function(win) { win.postMessage(message,'http://www.zhangxinxu.com'); }, 2000, e.source); break; case 'closed': clearInterval(interval); break; } }, false);
窗口B:
window.addEventListener('message', function(e) { document.querySelector("#message").innerHTML = "接受到的信息是:" + e.data;}, false);// 当文档加载完毕, 给父级来源发送信息。window.addEventListener('load', function(e){ e.currentTarget.opener.postMessage('ready', 'http://www.zhangxinxu.com');}, false);// 当窗体关闭,告诉父级窗体已经关闭了。window.addEventListener('unload', function(e){ e.currentTarget.opener.postMessage('closed', 'http://www.zhangxinxu.com');}, false);
代码分析:子窗口可以使用window.opener来引用父窗口
例子2:通过iframe打开无标题文档
代码分析:一个页面可以通过iframe来嵌套另外一个页面,被包含的页面又可以继续通过iframe标签也继续嵌套其他页面,这样就是一种层级关系。
我们知道window指的是当前页面窗口,那么parent指的是父级窗口,top指的是顶级窗口,三者可能会表示相同窗口。
5. postMessage
otherWindow.postMessage(message, targetOrigin, [transfer]);otherWindow 其他窗口的一个引用,比如iframe的contentWindow属性、执行window.open返回的窗口对象、或者是命名过或数值索引的window.frames。message 将要发送到其他 window的数据。它将会被结构化克隆算法序列化。这意味着你可以不受什么限制的将数据对象安全的传送给目标窗口而无需自己序列化。targetOrigin 通过窗口的origin属性来指定哪些窗口能接收到消息事件,其值可以是字符串""(表示无限制)或者一个URI。在发送消息的时候,如果目标窗口的协议、主机地址或端口这三者的任意一项不匹配targetOrigin提供的值,那么消息就不会被发送;只有三者完全匹配,消息才会被发送。这个机制用来控制消息可以发送到哪些窗口;例如,当用postMessage传送密码时,这个参数就显得尤为重要,必须保证它的值与这条包含密码的信息的预期接受者的origin属性完全一致,来防止密码被恶意的第三方截获。如果你明确的知道消息应该发送到哪个窗口,那么请始终提供一个有确切值的targetOrigin,而不是。不提供确切的目标将导致数据泄露到任何对数据感兴趣的恶意站点。transfer 可选 是一串和message 同时传递的 Transferable 对象. 这些对象的所有权将被转移给消息的接收方,而发送一方将不再保有所有权。 /* * 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);
二、跨域下页面之间的通信
1、利用iframe和location.hash,数据直接暴露在了url中,数据容量和类型都有限;
2、HTML5中最炫酷的API之一:就是 跨文档消息传输Cross Document Messaging。高级浏览器Internet Explorer 8+, chrome,Firefox , Opera 和 Safari 都将支持这个功能。这个功能实现也非常简单主要包括接受信息的”message”事件和发送消息的”postMessage”方法; 3、Window.name通信。(有专门的文章进行讲解) 例如: 发送消息的”postMessage”方法 向外界窗口发送消息:otherWindow.postMessage(message, targetOrigin);
代码分析:
otherWindow: 指目标窗口,也就是给哪个window发消息,是 window.frames 属性的成员或者由 window.open 方法创建的窗口。 参数说明: message: 是要发送的消息,类型为 String、Object (IE8、9 不支持) targetOrigin: 是限定消息接收范围,不限制请使用 ‘*’。 接受信息的”message”事件:var onmessage = function (event) { var data = event.data; var origin = event.origin; //do someing};if (typeof window.addEventListener != 'undefined') { window.addEventListener('message', onmessage, false);} else if (typeof window.attachEvent != 'undefined') { //for ie window.attachEvent('onmessage', onmessage);}
代码分析:
回调函数第一个参数接收 Event 对象,有三个常用属性: data: 消息 origin: 消息来源地址 source: 源 DOMWindow 对象转载地址:https://blog.csdn.net/yyychocolate/article/details/108275191 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!