当前位置:网站首页>Communication between browser tab pages
Communication between browser tab pages
2022-07-01 22:24:00 【yanling. zhang】
Reprint :https://zhuanlan.zhihu.com/p/63422027
1. scene 1: Two need to interact tab Pages have dependencies – postMessage
Such as A Page through JavaScript Of window.open open B page , perhaps B Page passing iframe Embedded in A page , This situation is the simplest , Can pass HTML5 Of window.postMessage API Complete communication , because postMessage The function is bound to window Under the global object , Therefore, there must be a page in the communication page ( Such as A page ) You can get another page ( Such as B page ) Of window object , Only in this way can one-way communication be completed ;B No need to get the page A Page window object , if necessary B Page face A Page communication , Only need B Page listening message event , Get the source object , The object is A page window References to objects :
2. scene 2: The two open pages belong to the same category – storage
To achieve two unrelated homologies tab Page correspondence , You can use a more ingenious way :localstorage.localStorage The storage of follows the same origin strategy , So two homologous tab Pages can be shared through this localStorage To achieve communication , By agreement localStorage One of the itemName, Based on this key The content of the value is as “ Shared hard disk ” Means of communication .
HTML5 Provides storage event , adopt window Object listening storage event , Listen localStorage Object change events ( Include item The addition of 、 Modification and deletion ). therefore , An efficient communication mechanism can be achieved through events .
边栏推荐
猜你喜欢
随机推荐
辅音和声母的区别?(声母与辅音的区别)
焱融看 | 混合云时代下,如何制定多云策略
杰理之蓝牙耳机品控和生产技巧【篇】
选择在同花顺上炒股开户可以吗?安全吗?
基于YOLOv5的口罩佩戴检测方法
月入1W+的自媒体达人都会用到的运营工具
vscode的使用
[commercial terminal simulation solution] Shanghai daoning brings you Georgia introduction, trial and tutorial
Go — 相关依赖对应的exe
PCB plug hole technology~
【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!
MySQL清空表数据
杰理之蓝牙耳机品控和生产技巧【篇】
GCC编译
灵动微 MM32 多路ADC-DMA配置
MySQL系列之事务日志Redo log学习笔记
MQ学习笔记
Manually implement function isinstanceof (child, parent)
CNN卷积神经网络原理讲解+图片识别应用(附源码)[通俗易懂]
MQ learning notes







