Get Browser Tab Index/Id

2019-02-21 15:16发布

问题:

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

回答1:

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.



回答2:

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.



回答3:

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.



回答4:

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.



回答5:

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