当前位置:网站首页>浏览器tab页之间的通信
浏览器tab页之间的通信
2022-07-01 19:37:00 【yanling.zhang】
转载:https://zhuanlan.zhihu.com/p/63422027
1.场景1:两个需要交互的tab页面具有依赖关系 – postMessage
如 A页面中通过JavaScript的window.open打开B页面,或者B页面通过iframe嵌入至A页面,此种情形最简单,可以通过HTML5的 window.postMessage API完成通信,由于postMessage函数是绑定在 window 全局对象下,因此通信的页面中必须有一个页面(如A页面)可以获取另一个页面(如B页面)的window对象,这样才可以完成单向通信;B页面无需获取A页面的window对象,如果需要B页面对A页面的通信,只需要在B页面侦听message事件,获取事件中传递的source对象,该对象即为A页面window对象的引用:
2.场景2: 两个打开的页面属于同源范畴 – storage
若要实现两个互不相关的同源tab页面通信,可以使用一种比较巧妙的方式:localstorage。localStorage的存储遵循同源策略,因此同源的两个tab页面可以通过这种共享localStorage的方式实现通信,通过约定localStorage的某一个itemName,基于该key值的内容作为“共享硬盘”方式通信。
HTML5提供了storage事件,通过window对象侦听storage事件,会侦听localStorage对象的变化事件(包括item的添加、修改和删除)。因此,通过事件可以完成高效的通信机制。
边栏推荐
- 杰理之、产线装配环节【篇】
- Richview RichEdit srichviewedit PageSize page setup and synchronization
- 网上开户是安全的吗?新手可以开炒股账户吗。
- leetcode刷题:栈与队列05(逆波兰表达式求值)
- Common components of flask
- 安装mysql时出现:需要这两个包perl(Data::Dumper),perl(JSON)
- tensorflow 张量做卷积,输入量与卷积核维度的理解
- 目標檢測——Yolo系列
- 股票手机开户哪个app好,安全性较高的
- Penetration tools - trustedsec's penetration testing framework (PTF)
猜你喜欢

PLC模拟量输入 模拟量转换FB S_ITR(三菱FX3U)

Exclusive news: Alibaba cloud quietly launched RPA cloud computer and has opened cooperation with many RPA manufacturers

柒微自动发卡系统源码

What if the win11 shortcut key switching input method doesn't respond? Shortcut key switching input method does not respond

小鸟逃票登机,如何反思,应如何解决,飞机为何怕小鸟?

杰理之、产线装配环节【篇】

EDA工具对芯片产业的重要性知识科普

NSI脚本的测试

深度学习 常见的损失函数

Penetration tools - trustedsec's penetration testing framework (PTF)
随机推荐
开环和闭环是什么意思?
Stack overflow 2022 developer survey: where is the industry going?
Comprehensive evaluation and detailed inventory of high-quality note taking software (I) note, obsedian, remnote, flowus
Develop those things: easycvr platform adds playback address authentication function
架构师毕业总结
leetcode刷题:栈与队列07(滑动窗口最大值)
考虑关系的图卷积神经网络R-GCN的一些理解以及DGL官方代码的一些讲解
想请教一下,券商选哪个比较好尼?本人小白不懂,现在网上开户安全么?
Keras machine translation practice
喜马拉雅自研网关架构演进过程
运动捕捉系统原理
2022熔化焊接与热切割上岗证题目模拟考试平台操作
【Leetcode】最大连续1的个数
多个张量与多个卷积核做卷积运算的输出结果
Review notes of Zhang Haifan in introduction to software engineering (Sixth Edition)
人脸识别系统 —— OpenCV人脸检测
2022安全员-A证考题及在线模拟考试
深度学习 神经网络基础
[mysql] install mysql5.7
[Mysql]安装Mysql5.7