当前位置:网站首页>浏览器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的添加、修改和删除)。因此,通过事件可以完成高效的通信机制。
边栏推荐
- [multithreading] realize the singleton mode (hungry and lazy) realize the thread safe singleton mode (double validation lock)
- Is it safe to open an account online? Can a novice open a stock trading account.
- EURA eurui E1000 series inverter uses PID to realize the relevant parameter setting and wiring of constant pressure water supply function
- 安装mysql时出现:需要这两个包perl(Data::Dumper),perl(JSON)
- 网上开户是安全的吗?新手可以开炒股账户吗。
- 深度学习 神经网络基础
- 2022熔化焊接与热切割上岗证题目模拟考试平台操作
- 目标检测——Yolo系列
- leetcode刷题:二叉树02(二叉树的中序遍历)
- PHP gets the external chain address of wechat applet and applet store
猜你喜欢
如何用OpenMesh创建一个四棱锥
EMC-电路保护器件-防浪涌及冲击电流用
leetcode刷题:二叉树02(二叉树的中序遍历)
Learn white box test case design from simple to deep
杰理之、产线装配环节【篇】
What if win11 can't pause the update? Win11 pause update is gray. How to solve it?
考虑关系的图卷积神经网络R-GCN的一些理解以及DGL官方代码的一些讲解
Customize the insertion of page labels and realize the initial search of similar address books
Win11 how to hide the taskbar? Win11 method to hide the taskbar
Importance of EDA tools to chip industry knowledge popularization
随机推荐
Swiftui 4 new features complete toggle and mixed toggle multiple binding components
[C language] explain the usage of memset() function in detail
Target detection - Yolo series
喜马拉雅自研网关架构演进过程
Uniapp uses Tencent map to select points without window monitoring to return users' location information. How to deal with it
Internship: gradually moving towards project development
从20s优化到500ms,我用了这三招
目標檢測——Yolo系列
《软件工程导论(第六版)》 张海藩 复习笔记
Customize the insertion of page labels and realize the initial search of similar address books
Common components of flask
关联线探究,如何连接流程图的两个节点
人才近悦远来,望城区夯实“强省会”智力底座
Internship: complex JSON format data compilation interface
cmake工程化相关
There are four ways to write switch, you know
收藏:存储知识全面总结
300题线性代数 第四讲 线性方程组
深度学习 常见的损失函数