Get Browser Tab Index/Id
Asked Answered
P

5

10

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

Plotinus answered 22/12, 2010 at 15:0 Comment(8)
What would be a use case for such information (outside of browser addons)?Sherrer
It's more of a question of can this be done then use case. I was asked the question and didn't see anything on the subject, got me thinking if it could be done at all. On the Server Side I don't think I could identify individual tabs as each tab is treated as a page load request, but I was thinking JavaScript might be able to have access to this information on the browser.Plotinus
Personally, If I enter a website and a popup or whatever shows up saying "You have tab 1 active" or anything of the same sort. I would switch browsers until such thing is fixed, and boycott the website I am using. :DThimblerig
@Dan, LOL I would as well, this is more of an a internal site and someone higher up asking the question. Just wanted to give an honest answer and just curious if it "Could" be done ;)Plotinus
+1 Where would we be without that can-it-be-done attitude? Nowhere.Duodecimo
#1029130Amoritta
@Amoritta looks interesting, thanksPlotinus
I would recommend change the message, instead of "Please use Tab 2 as this is Tab 1" use "The site is already open in other tab please go there" ;-)Glaser
N
7

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.

Nataline answered 12/1, 2011 at 14:48 Comment(1)
Thanks, I understand PHP is a server side language but I was thinking that maybe an environmental variable might be set like USER_AGENT that might give some clue(s). I will have to try the cookie page counter idea, thanksPlotinus
P
1

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.

Prophet answered 7/1, 2011 at 0:31 Comment(0)
A
0

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.

Antistrophe answered 22/12, 2010 at 15:3 Comment(1)
Thanks, I do agree with you that this might not be possible even from a client side language. Someone asked the questions, stuck in my head, just wanted to know. ThanksPlotinus
P
0

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.

Phosphide answered 14/1, 2011 at 6:35 Comment(0)
D
0

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

Doxology answered 15/1, 2011 at 10:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.