如何在浏览器标签之间共享数据? [英] How to share data between browser tabs?

查看:58
本文介绍了如何在浏览器标签之间共享数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须开发一个程序,在该程序中我需要从服务器获取事件并想要更新网页.

I have to develop a program where I need to get the event from server and want to update the web page.

例如我打开了多个用于聊天的浏览器选项卡,每个聊天页面都有其自己的线程来从服务器获取事件,但是一旦聊天中的一个线程将比其他线程获取事件,则由于另一个聊天页面所提取的内容,其他线程将获得空的事件堆栈

e.g. I have multiple browser tab opened for chat and each chat page have its own thread to fetch the event from the server but once the one thread among the chat will fetch the event than other get empty stack of event due to extract by the another Chat page.

因此,我的关注点是与多个聊天页面共享事件,而无需刷新页面.

So, here my concern is to share event with multiple chat pages without refreshing the page.

推荐答案

听起来像您可以使用 postMessage ,让获取邮件的标签与您域中的所有其他页面共享.

Sound like you could use postMessage to have the tab that gets the message share it with all the other pages on your domain.

postMessage(JSON.stringify({chatmsg: someVarFromServer}), "http://www.mydomain.com");

让其他聊天页面监听消息事件:

Have other chat pages listen for message events:

window.addEventListener("message", receiveMessage, false);

function receiveMessage(event) {
    // reject foreign messages!
    if (event.origin !== "http://www.mydomain.com") return;

    var message = JSON.parse(event.data).chatmsg;

    // this other page now has the message
}

这篇关于如何在浏览器标签之间共享数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆