Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Browser Tab Index/Id

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

like image 572
Phill Pafford Avatar asked Dec 22 '10 15:12

Phill Pafford


People also ask

How do I get the tab name in Chrome?

Quick rename can be done by right-clicking anywhere in the page and click on "Rename Tab".

What is tab in API?

Tabs let a user open several web pages in their browser window and then switch between those web pages. With the Tabs API, you can work with and manipulate these tabs to create utilities that provide users with new ways to work with tabs or to deliver the features of your extension.


2 Answers

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.

like image 42
mrbinky3000 Avatar answered Oct 07 '22 19:10

mrbinky3000


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.

like image 151
Ryan Ballantyne Avatar answered Oct 07 '22 19:10

Ryan Ballantyne