views:

754

answers:

9

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.

+3  A: 

I can't imagine how you could do this, nor can I imagine why you'd want to. Whether to open in a new tab, or a new window, or wherever should be a decision made by the end user, free from any restrictions placed on him by your application.

John Saunders
+1 for that. It's like all those applications that insist on pinning themselves to the most-used list, whacking themselves on the desktop, running at startup in the "system tray", checking the net every seven minutes for an update, and so on. When are people going to get it through their thick skulls that the computer belongs to the user? If they think your software or web page is so vitally important, they'll set it up like that. Otherwise, shove off and leave their computer alone! :-) It should be up to them whether they open your link in a new browser, or tab, or just wget it to view later.
paxdiablo
+1  A: 

no, you cannot. Moreover, some browsers don't have tabs at all.

Luixv
A: 

I'm pretty sure this is not possible.

Buddy
A: 

I really don't think so. The closest you could get as far as I'm aware of is monitoring keypress and mouseclick events to try to match actions to common methods for opening links in new tabs. (ie. if they're holding Command when they click, or middle-clicked, it's probably a new tab). That wouldn't be reliable though, as any of those bindings can be changed.

Burke
+11  A: 

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.

RichieHindle
You could also look at history.previous...
Jason Punyon
@Jason: I get permission errors in Firefox when I try to look at history.previous.
RichieHindle
@RichieHindle: hmmm...I was trying to put my finger on it...but yep...that's why I suck...
Jason Punyon
+1  A: 

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.

Tracker1
+1  A: 

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.

Paul Alexander
+1 for using window.opener
Andrew Duffy
window.opener is null for me in Firefox when I "Open Link in New Tab".
RichieHindle
window.opener only gets set when using a js window.open, not via user action on an A tag.
Tracker1
Ahh....you're right. Boo for me. Forgot about that.
Paul Alexander
A: 

It is client behaviour, so I think you could do something with javascript, like check for browser history, but it is ambiguous between a new tab and a new windows.

Beside that, not all browsers has tabs and not all browsers has the same meaning for what a tab is (in some are different process, in some others they aren't).

But, why would you want to check that? It really matter if your application is executing in a new tab or not? I don't think so...

eKek0
+3  A: 

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)
scunliffe