用户在其中一个注销时自动注销所有打开的选项卡 [英] Logout all open tabs automatically when user logs out in one of them

查看:101
本文介绍了用户在其中一个注销时自动注销所有打开的选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经根据localStorage创建了登录页面。
在加载页面时,我检查了localStorage的值。
如果我在多个选项卡中打开网页,然后从任何一个选项卡注销,则所有其余页面应自动注销。

I have created Login page, based on localStorage. On loading the page I have checked the value of localStorage. If I opened the web page in more than one tab and then I logout from any one of the tabs, all the remaining pages should logout automatically.

如果重新加载/刷新意味着它注销。

If reload/refresh means it logging out.

请帮助我运行脚本,当用户查看页面或说任何其他方式解决此问题。

Please help me to run a script, when user view the page or say any other way to solve this problem.

推荐答案

您可以使用存储事件
$ b

You can use Storage events to be notified when localStorage values are changed.

function storageChange (event) {
    if(event.key === 'logged_in') {
        alert('Logged in: ' + event.newValue)
    }
}
window.addEventListener('storage', storageChange, false)

例如,如果其中一个选项卡退出:

If, for example, one of the tabs logs out:

window.localStorage.setItem('logged_in', false)

然后所有其他选项卡将收到一个 StorageEv ent ,并且会出现提示:

Then all other tabs will receive a StorageEvent, and an alert will appear:

Logged in: false

我希望这能回答您的问题!

I hope this answers your question!

这篇关于用户在其中一个注销时自动注销所有打开的选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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