[英]How to know if browser tab is already open using Javascript?
How to know or check if the two browser tab is already open and if those tab are open, the user will receive an alert box or msg box saying that 'the url is already open', something like that, in pure/native JavaScript?如何知道或检查两个浏览器选项卡是否已打开,如果这些选项卡已打开,用户将收到一个警告框或消息框,说“url 已打开”,类似于纯/原生 JavaScript? This browser tab is contain an external website which is I don't have any privileges to manipulate or change it.
此浏览器选项卡包含一个外部网站,我无权操作或更改它。 Thanks
谢谢
Example URLs示例 URL
yahoo.com and google.com
I want to alert the user if there's already open tab for yahoo.com and google.com如果 yahoo.com 和 google.com 已经打开标签,我想提醒用户
And I want to use tabCreate to open the url like this:我想使用 tabCreate 打开这样的网址:
tabCreate("http://maps.google.com/", "tabMapsPermanentAddress");
mean to open a new tab, it is use in creating chrome extension
You may use something like following您可以使用以下内容
<!-- HTML -->
<a id="opener">Open window</a>
// JavaScript
var a = document.getElementById('opener'), w;
a.onclick = function() {
if (!w || w.closed) {
w = window.open("https://www.google.com","_blank","menubar = 0, scrollbars = 0");
} else {
console.log('window is already opened');
}
w.focus();
};
Working jsBin |工作jsBin |More on window.open method
更多关于 window.open 方法
If you want to control more than one window, use the snippet below如果您想控制多个窗口,请使用以下代码段
<!-- HTML -->
<a href="https://www.google.com" class="opener">Open google.com</a> |
<a href="http://www.yahoo.com" class="opener">Open yahoo.com</a>
//JavaScript
window.onload = function(){
var a = document.querySelectorAll('.opener'), w = [], url, random, i;
for(i = 0; i < a.length; i++){
(function(i){
a[i].onclick = function(e) {
if (!w[i] || w[i].closed) {
url = this.href;
random = Math.floor((Math.random() * 100) + 1);
w[i] = window.open(url, "_blank", random, "menubar = 0, scrollbars = 0");
} else {
console.log('window ' + url + ' is already opened');
}
e.preventDefault();
w[i].focus();
};
})(i);
}
};
If you don't want them to load in separated window, just exclude this line如果您不希望它们在单独的窗口中加载,只需排除此行
random = Math.floor((Math.random()*100)+1);
and remove random
reference from the next line并从下一行中删除
random
引用
w[i] = window.open(url, "_blank", random, "menubar=0,scrollbars=0");
Side note: As you can see above, we created two windows with some third party content;旁注:正如您在上面看到的,我们创建了两个包含一些第三方内容的窗口; you should know that there's no way to get any reference (to the parent/opener window) from them.
你应该知道没有办法从他们那里得到任何引用(到父窗口/打开窗口)。
One basic idea is to store the tab count in either a cookie or localStorage
, incrementing it on page load and decrementing it on page unload:一个基本的想法是将标签计数存储在 cookie 或
localStorage
,在页面加载时增加它并在页面卸载时减少它:
if (+localStorage.tabCount > 0)
alert('Already open!');
else
localStorage.tabCount = 0;
localStorage.tabCount = +localStorage.tabCount + 1;
window.onunload = function () {
localStorage.tabCount = +localStorage.tabCount - 1;
};
Try opening this fiddle in multiple tabs.尝试在多个选项卡中打开这个小提琴。
Note that this technique is pretty fragile, though.但是请注意,这种技术非常脆弱。 For example, if for some reason the browser crashes, the unload handler won't run, and it'll go out of sync.
例如,如果由于某种原因浏览器崩溃,卸载处理程序将不会运行,并且会失去同步。
The answer by Casey Chu works fine until the browser crashes with the page open. Casey Chu的回答工作正常,直到浏览器在页面打开时崩溃。 On any next execution, the
localStorage
object will have initialized tabCount
with non zero value.在下一次执行时,
localStorage
对象将使用非零值初始化tabCount
。 Therefore a better solution is to store the value in a session cookie.因此,更好的解决方案是将值存储在会话 cookie 中。 The session cookie will be removed when browser exits successfully.
当浏览器成功退出时,会话 cookie 将被删除。 When the browser crashes the session cookie will actually be preserved but fortunately only for one next execution of the browser.
当浏览器崩溃时,会话 cookie 实际上会被保留,但幸运的是只用于浏览器的下一次执行。
Object sessionStorage
is distinct for each tab so it cannot be used for sharing tab count.每个选项卡的对象
sessionStorage
都是不同的,因此它不能用于共享选项卡计数。
This is the improved solution using js-cookie library.这是使用js-cookie库的改进解决方案。
if (+Cookies.get('tabs') > 0)
alert('Already open!');
else
Cookies.set('tabs', 0);
Cookies.set('tabs', +Cookies.get('tabs') + 1);
window.onunload = function () {
Cookies.set('tabs', +Cookies.get('tabs') - 1);
};
This answer: https://stackoverflow.com/a/28230846 is an alternative that doesn't require Cookies/js-cookie library.这个答案: https : //stackoverflow.com/a/28230846是一种不需要 Cookies/js-cookie 库的替代方法。 It better suited my needs.
它更适合我的需求。 In a nutshell (see linked answer for full description):
简而言之(有关完整说明,请参阅链接答案):
$(window).on('storage', message_receive);
... ...
// use local storage for messaging. Set message in local storage and clear it right away
// This is a safe way how to communicate with other tabs while not leaving any traces
//
function message_broadcast(message)
{
localStorage.setItem('message',JSON.stringify(message));
localStorage.removeItem('message');
}
// receive message
//
function message_receive(ev)
{
if (ev.originalEvent.key!='message') return; // ignore other keys
var message=JSON.parse(ev.originalEvent.newValue);
if (!message) return; // ignore empty msg or msg reset
// here you act on messages.
// you can send objects like { 'command': 'doit', 'data': 'abcd' }
if (message.command == 'doit') alert(message.data);
// etc.
}
Just going to throw this up here, because I wish I had something like it.只是想把它扔在这里,因为我希望我有类似的东西。 Make what you will of it.
随心所欲。
If you want a solution for checking if you are the active tab that doesn't require a cookie, works as a React hook, and works whether or not the browser crashes, you can use this useIsActiveTab
webhook which returns true if you are the most recent active tab/window.如果您想要一个解决方案来检查您是否是不需要 cookie、作为 React 钩子工作以及浏览器是否崩溃的活动选项卡,您可以使用这个
useIsActiveTab
webhook,如果您是最最近的活动选项卡/窗口。 You can also set yourself as the active tab with activateTab
.您还可以使用
activateTab
将自己设置为活动选项卡。
import { useEffect, useState } from 'react';
const CHARACTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const CHARACTERS_LENGTH = CHARACTERS.length;
function generateTabId() {
let result = '';
const prefix = 'TAB_';
const length = 15;
for (let i = 0; i < length - prefix.length; i++) {
result += CHARACTERS.charAt(Math.floor(Math.random() * CHARACTERS_LENGTH));
}
if (prefix.includes('_')) {
return `${prefix}${result}`;
}
return `${prefix}_${result}`;
};
const tabId = generateTabId();
export function activateTab(): void {
localStorage.setItem('activeTab', tabId);
const event = new Event('thisStorage');
window.dispatchEvent(event);
}
export function useIsActiveTab(): boolean {
const [isActiveTab, setIsActiveTab] = useState(false);
useEffect(() => {
setActiveTab();
function updateIsActiveTab() {
setIsActiveTab(checkIfActiveTab());
}
window.addEventListener('storage', updateIsActiveTab);
window.addEventListener('thisStorage', updateIsActiveTab);
updateIsActiveTab();
return () => {
window.removeEventListener('storage', updateIsActiveTab);
window.removeEventListener('thisStorage', updateIsActiveTab);
};
}, []);
return isActiveTab;
}
function checkIfActiveTab(): boolean {
const activeTab = localStorage.getItem('activeTab');
if (!activeTab) {
console.error('localStorage.activeTab is not set');
return true;
}
if (activeTab === tabId) {
return true;
}
return false;
}
function setActiveTab(): void {
localStorage.setItem('activeTab', tabId);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.