+
95
-

浏览器窗口之间如何进行消息传递?

浏览器窗口之间如何进行消息传递?


网友回复

+
15
-

浏览器窗口之间可以通过以下三种方式进行消息传递:

LocalStorage / SessionStorage

LocalStorage 和 SessionStorage 是浏览器提供的本地存储机制,可以在不同的浏览器窗口之间共享数据。当一个窗口修改了存储的值时,其他窗口会触发 storage 事件,可以通过该事件接收到其他窗口的数据。
// 发送消息
localStorage.setItem('message', 'hello');

// 接收消息
window.addEventListener('storage', function(event) {
if (event.key === 'message') {
console.log(event.newValue); // 'hello'
}
});

BroadcastChannel

BroadcastChannel 是...

点击查看剩余70%

我知道答案,我要回答