如何防止同一个网站打开多个标签页? [英] How to prevent same website open multiple tab pages?

查看:105
本文介绍了如何防止同一个网站打开多个标签页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景,可以在浏览器的多个打开的标签页中阻止同一个网站.我的想法是当用户第一次从浏览器打开一个网站时,没关系,当用户生成新的标签页或外部链接再次打开同一个网站时,我们应该将其重定向到已经打开的网站.我不知道如何实施.我可以有一些线索吗?谢谢.

I have a scenario that prevents the same website in multiple opened tab pages of browser. My idea is when user open a website at first time from browser, it's fine, when user produces the new tab page or external link to open the same website again, we should redirect it to already opened one. I have no idea how to implement. Can I have some clues? Thanks.

推荐答案

我最近遇到了一个类似的问题,我不得不阻止在同一个 localStorage 上操作多个窗口/选项卡.

I recently ran across a similar problem where I had to prevent that multiple windows/tabs operated on the same localStorage.

解决方案是使用 StorageEvents:每个窗口每当另一个 (!) 窗口更改与此窗口使用的相同的 localStorage 时,都会收到一个 StorageEvent.

The solution was to use StorageEvents: every window receives a StorageEvent whenever another(!) window made changes to the same localStorage as this one uses.

因此,诀窍是为 localStorage 定义一个虚拟"键,并让每个窗口向该键写入一些随机值,以便让所有其他使用相同 localStorage 的窗口接收一个 StorageEvent:

Thus, the trick was to define a "dummy" key for localStorage and let every window write some random value into that key just to let all other windows using the same localStorage receive a StorageEvent:

  window.addEventListener('storage', () => {
    window.alert('another window or tab is working on the same localStorage')
  }, false)

  localStorage.setItem('Sentinel',Math.random())

这篇关于如何防止同一个网站打开多个标签页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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