Is it possible to detect if a user has opened a link in a new tab?

If a user is on your website and opens another link (also to your website) in a new tab, is it possible to differentiate this from the user just clicking on the link normally? This can be in javascript, on the server, whatever.

I'm guessing that the answer is that you cannot do this, but I wanted to double check.


You can sort of do it like this:

if (history.length == 1) {  // Um, needs to be 0 for IE, 1 for Firefox
    // This is a new window or a new tab.
}

There may be other ways for history.length to be 1, but I don't know what they might be.


In addition to history.length in JavaScript you can read/write the window's name.

Thus if you check if it has a name onload... it should be blank on the very first load... if you then set it to "foo"... on each subsequent load in that window... the window.name property will return "foo"... unless you open a link in a new tab/window... that new window should have no name set.

(unless of course you open a popup via window.open(url, name, features); which allows you to pre-set the name)

<script>
 if(window.name == ''){
   //first load (or Nth load in a new Tab/Window)
   if(!SOME_VALUE_SET_FOR_2ND_TO_NTH_LOADS){
     //set name so we can catch new Tab/Window
     window.name = 'myWinName';
   } else {
     //we have a new Tab/Window (or something funky)
     alert('What?! One window not cool enough for ya?\n' +
       'Calling the InterWeb Police!');
   }
 } else if(window.name == 'myWinName'){
   //2nd-Nth load
   document.title = 'All is well... we think';
 }
</script>

Caveats:

  • If your page is initially loaded in a window/frame that already had a name... things will get quirky
  • If your page has (named) iframes and you have any links targeted into those iframes, there is a bug in IE7/8 whereby when the user opens those links in a new tab/window, the new tab/window will "inherit" the name of the iframe that was originally targeted (very ODD bug with no fix ever expected)

This is what I use in ASP.NET MVC to forbid authenticated users to open multiple tabs:

<script language="javascript" type="text/javascript">
    @if(Request.IsAuthenticated)
    {
        <text>
        if (window.name != 'singleWindow') {
            window.location.href = "Content/ErrorPages/SingleTab.htm";
        } 
        </text>
    }
    else
    {
        <text>
        window.name = "singleWindow";
        </text>
    }
</script>

Basically, this sets the window name first time when the user visits the login page. After logging in, for each subsequent page load the window name is tested.

Two problems:

  • does not wok if JavaScript disabled
  • if by mistake the user closes the original tab and then pastes some other link to my website in the address bar, the user will always receive the error page. To give the user a chance to recover, I have included "Log out" link in the SingleTab.htm page, so the user can destroy his session cookie and start a new session.

The window.opener property in JavaScript will point to the window that opened the new window. However it doesn't distinguish between a new window and a new tab. Tabs are not part of the official W3C spec so there's no direct support for them.


The short answer is, no. The long answer is, if you are doing an ajaxy call from your pages to server-side methods, that could keep ttrack of the windows open (called within a short timeframe). It would be a sloppy, unreliable mess, and you couldn't differentiate between a new window or a tab for that matter.