获取浏览器选项卡索引/Id

发布于 2024-10-08 16:38:59 字数 323 浏览 4 评论 0原文

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

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

伪代码:

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

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

有趣的是,我搜索的有关选项卡的所有内容都与网页本身的选项卡索引相关,叹息......

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

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?

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无畏 2024-10-15 16:38:59

严格来说。 TABS 位于最终用户的计算机上。 PHP 在服务器上运行。 PHP 无法看到最终用户的计算机正在做什么,它只能服务最终用户 PHP 化的页面。

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

研究一下 jQuery。

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 serve the end user PHP'ed pages.

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.

Look into jQuery.

风苍溪 2024-10-15 16:38:59

至于确定绝对选项卡索引,我知道没有办法用 Javascript 来做到这一点。您可以通过名称来识别窗口,但不能通过其他任何东西来识别。

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

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

有关如何使用 Cookie 的详细信息,请参阅浏览器选项卡/窗口之间的 Javascript 通信使用 Javascript 在窗口之间进行通信。

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.

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.

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.

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

深海里的那抹蓝 2024-10-15 16:38:59

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

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

我认为你最好的机会(如果有的话)是使用其他客户端语言,如 flash、silverlight 或 java 插件,因为这些语言可以比 javascript 做更多的事情 - 但很抱歉我不这样做不知道其中任何一个足以提供更多信息或提示。

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

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).

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.

寒尘 2024-10-15 16:38:59

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

尝试寻找一种替代方法,正如其他一些人在评论中建议的那样。

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.

千秋岁 2024-10-15 16:38:59

我确信没有全局变量来支持该信息。但也许像 Firefox 或 Google Chrome 这样聪明的浏览器可能会支持它的某些功能。我在网上快速搜索了一下,然后就得到了这些。

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

第二个是,如果您想将其用于自己的用途,那么它可能有用,我不测试它。这是一个插件。这是 打开标签计数 Mozilla 插件

打开标签预览

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.

First, check Mozilla Tab Helper can 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.

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 Preview

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文