Javascript 获取浏览器标签索引/ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4510454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 12:43:09  来源:igfitidea点击:

Get Browser Tab Index/Id

phpjavascriptbrowserbrowser-tab

提问by Phill Pafford

So in most current browsers there is the feature of Tabs, is there a way to get the Tab index?

那么目前大部分浏览器都有Tabs的功能,有没有办法获取Tab索引呢?

so Tab 1 has www.google.com opened in it and Tab 2 has www.google.com opened in it, is there a way to identify what the Tab index is?

所以Tab 1在其中打开了www.google.com,Tab 2在其中打开了www.google.com,有没有办法识别Tab索引是什么?

Pseudo Code:

伪代码:

if($tab == 2) {
  alert "Tab 2 is active\n";
}

if($tab == 1) {
  alert "Please use Tab 2 as this is Tab 1\n";
}

Funny as everything I search for about tabs is related to the tab index of the webpage itself, sigh...

有趣的是,我搜索关于标签的所有内容都与网页本身的标签索引有关,叹息......

采纳答案by mrbinky3000

Strictly speaking. TABS are on the end user's machine. PHP works on the server. PHP can't see what the end user's machine is doing, it can only servethe end user PHP'ed pages.

严格来讲。TABS 位于最终用户的计算机上。PHP 在服务器上工作。PHP 无法看到最终用户的机器在做什么,它只能最终用户的 PHP 页面提供服务

Google does this with JavaScript and Cookies. For every instance of the page opened, increment a cookie counter. If the counter > 1, use AJAX to display an error message. Also, prohibit the page from functioning if cookies or JavaScript is disabled.

Google 使用 JavaScript 和 Cookie 来实现这一点。对于打开的每个页面实例,增加一个 cookie 计数器。如果计数器 > 1,则使用 AJAX 显示错误消息。此外,如果禁用 cookie 或 JavaScript,则禁止页面运行。

Look into jQuery.

看看jQuery。

回答by Samnan

Don't waste anymore time on this mate. It isn't possible, mainly because any webpage inside browser will not be able to get this kind of information due to security restrictions.

不要再在这个伙伴身上浪费时间了。这是不可能的,主要是因为安全限制,浏览器中的任何网页都无法获取此类信息。

Try looking for an laternative approach as some of the other guys have suggested in their comments.

尝试寻找其他一些人在评论中建议的替代方法。

回答by Fatih Acet

I am sure there is not a global variable for support that information. But maybe clever browsers such as Firefox or Google Chrome might support something on it. I have made a quick search on net and I came with these.

我确信没有支持该信息的全局变量。但也许聪明的浏览器,如 Firefox 或 Google Chrome 可能会支持它。我在网上进行了快速搜索,我带着这些来了。

First, check Mozilla Tab Helpercan be work with Mozilla. But be remember, this will never be a cross browser solution. Also, I am thinking there is not a cross browser solution.

首先,检查Mozilla Tab Helper 是否可以与 Mozilla 一起使用。但请记住,这永远不会是跨浏览器的解决方案。另外,我认为没有跨浏览器解决方案。

Second one is, if you want to use this for your own use then it might bu useful, I don't test it. This is a addon. Here is the Open Tab Count Mozilla Addon

第二个是,如果您想将其用于自己的用途,那么它可能很有用,我不对其进行测试。这是一个插件。这是Open Tab Count Mozilla 插件

Open Tab Preview

打开标签预览

回答by oezi

in php: definitely not - it's executed on your server without access to the cleints browser.

在 php 中:绝对不是 - 它在您的服务器上执行而无需访问客户端浏览器。

maybe there's a solution using javascript (but i've never heard of that, and i'm pretty sure this isn't possible too - at least not as a cross-browser solution).

也许有一个使用 javascript 的解决方案(但我从来没有听说过,我很确定这也是不可能的 - 至少不是作为跨浏览器的解决方案)。

i think the best chance you'll have (if there even is one) is using other client-side languages like flash, silverlight or a java-plugin as this ones can do a lot more than javascript - but i'm sorry i don't know any of these good enough to give more information or hints.

我认为您将拥有的最佳机会(如果有的话)是使用其他客户端语言,如 flash、silverlight 或 java 插件,因为这些语言可以做的比 javascript 多得多——但对不起,我不知道不知道这些足以提供更多信息或提示。

回答by Ryan Ballantyne

As far as determining the absolute tab index, I know of no way to do it with Javascript. You can identify windows by their names, but not anything else.

至于确定绝对标签索引,我知道无法用 Javascript 来做到这一点。您可以通过名称来识别窗口,但不能通过其他任何名称来识别。

In your example of two tabs containing the same web page, you should be able to uniquely identify them by making them aware of each other. You'd need to use cookies for this. Essentially, when a page is loaded, it would check for a cookie that tells it about other instances of the page that are currently loaded, and make decisions accordingly.

在包含同一网页的两个选项卡的示例中,您应该能够通过让它们相互了解来唯一地识别它们。为此,您需要使用 cookie。本质上,当一个页面被加载时,它会检查一个 cookie,该 cookie 会告诉它当前加载的页面的其他实例,并相应地做出决定。

In this scenario, your onload handler would check the cookies, and register the loading page. You'd also need an onunload handler to unset the cookie pertaining to the page being unloaded.

在这种情况下,您的 onload 处理程序将检查 cookie,并注册加载页面。您还需要一个 onunload 处理程序来取消设置与正在卸载的页面有关的 cookie。

See Javascript communication between browser tabs/windowsfor more information on how to use cookies to communicate between windows with Javascript.

有关如何使用 cookie 在带有 Javascript 的窗口之间进行通信的更多信息,请参阅浏览器选项卡/窗口之间的 Javascript 通信