Get Browser Tab Index/Id

Phill Pafford picture Phill Pafford · Dec 22, 2010 · Viewed 25.7k times · Source

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

Answer

mrbinky3000 picture mrbinky3000 · Jan 12, 2011

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.