Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 574
swampsjohn Avatar asked May 11 '09 21:05

swampsjohn


People also ask

How do I know if someone opens a link in a new tab?

You can examine the ctrlKey , shiftKey , and metaKey properties of the event object. If either is true, the key control, shift, or meta (Apple's command) key is being held and you should allow the default link action to proceed.

Is there a way to open links in a new tab without switching to that tab?

1) select "open link in new background tab" from the context menu (second choice), 2) use middle-click on the link with your mouse; 3) use Ctrl+leftclick on the link with your mouse, 4) designate a mouse gesture to open a background tab when you perform it over a link.

Why does a link open in a new tab?

Opening an external link in a new tab allows users to explore the other site as much as they want without having to hit the back button again and again to go back to your site. All they need to do is click the tab your site is on.

Which tag is used to open a URL in a new tab?

You just need an anchor ( <a> ) element with three important attributes: The href attribute set to the URL of the page you want to link to. The target attribute set to _blank , which tells the browser to open the link in a new tab/window, depending on the browser's settings.


2 Answers

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.

like image 73
RichieHindle Avatar answered Sep 30 '22 18:09

RichieHindle


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)
like image 26
scunliffe Avatar answered Sep 30 '22 18:09

scunliffe