40

When a user clicks on a link

<a href="http://www.stackoverflow.com" target="_blank">click</a>

is there a way to stay on the current window instead of going to the tab ?

Kyle Martin
  • 568
  • 2
  • 19
EnexoOnoma
  • 8,454
  • 18
  • 94
  • 179
  • 1
    This definitely isn't possible with PHP. – Nathan Sep 22 '11 at 23:15
  • Yes, either change the browser preferences or use whatever key and click combination is required for that browser to open the link in a new tab but keep focus on the current on. You can't do it with script. – RobG Sep 22 '11 at 23:17
  • 5
    Leave it up to the user. If they don't want to go to the new window right away, they can right click and select "Open in New Tab". It will open the tab in the background, at least it does for me. If I just plain clicked a link I would expect to be immediately showed that page. Bottom line, *don't irritate your visitors*. – animuson Sep 22 '11 at 23:50
  • 3
    what if user wants to open 10 links before actually going into them and not want to wait for each of them to load so he opens them all together and go read one while others load – Muhammad Umer Jan 28 '14 at 19:38
  • 2
    Middleclick opens a new tab, and the user can configure his browser to focus the new tab or not. **This is not something a website can/should decide** – ThiefMaster Jul 09 '14 at 07:18
  • Ctrl + clicking also opens a new tab in the background if you don't have a mouse to middle click. – rmutalik May 19 '19 at 06:36
  • Here you go: [automacticlly switches to a new link opened in a new tab even with that option turned off in the settings](https://support.mozilla.org/en-US/questions/1327563) – kmario23 Jan 01 '22 at 01:15

8 Answers8

6

I guess target="_blank" would open new tab/Windows but will switch the tab as well, and no way I can find them stuff in html, Yes but when we click in link pressing control key it opens the link in new background tab, Using javascript we can stimulate same Here is code I found

function openNewBackgroundTab(){    
    var a = document.createElement("a");    
    a.href = "http://www.google.com/";    
    var evt = document.createEvent("MouseEvents");    

    //the tenth parameter of initMouseEvent sets ctrl key    
    evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0,true, false, false, false, 0, null);    
    a.dispatchEvent(evt);
}
Adel Bachene
  • 974
  • 1
  • 14
  • 34
  • Knowledge of that + behavior is precisely what I want here, thx! Will tell that to the users and issue solved. After all we do *not* want to tinker too much with the ways browsers work. IMHO that worsens user experience more than most other things we do. – Markus-Hermann May 12 '20 at 06:27
  • It has to be noted that this only works in Chrome and only if this is executed in the same stack as the original click event. A simple setTimeout will break this behaviour causing window.open to be called after a timeout or interval to always be in the foreground. See https://codepen.io/jeffreyvn/pen/BapLoqQ for a small demonstration. – Jeffrey van Norden Mar 29 '21 at 12:51
5
<a href="www.stackoverflow.com" onclick="window.open('#','_blank');window.open(this.href,'_self');">

This will load the current web page in a new tab which the browser will focus on, and then load the href in the current tab

Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
Jamie
  • 67
  • 1
  • 2
  • 5
    Please not. This might very actually open a new window or trigger a popup blocker. – ThiefMaster Jul 09 '14 at 07:19
  • 1
    Although it technically works, this solution may fail in some particular cases: 1) it may not work if the user has a pop-up blocker; 2) If the URL contains a # value, it will be lost; 3) As the current page is reloaded in a new tab, the scripts/download/actions that ran on load will run again; 4) The current page is lost, if the user was in the middle of an action (e.g. filling a form), the information will be lost too. Some of these can be avoided by changing/adding more code (e.g. #2 would be solved by doing `window.open(window.location.href,'_blank')` instead), but it may be a hassle – Alvaro Montoro May 20 '16 at 13:31
1

Try this (I found it useful for playing audio files in the background without distracting the user from the current page or using script.)

<a href="first.mp3" target="yourframename"> First Song </a>
<a href="second.mp3" target="yourframename"> Second Song </a>

The first time a user clicks on the link, the target window will be on top. Any subsequent clicks leave the current window on top. Essentially, the links open in the background window because there is no <frame> or <iframe> specified.

Only works on Opera, Mozilla and IE (the versions on my computer). Doesn't work for Chrome and Safari.

1

You could open the current page in a new tab, as this new tab gets focused it would seem you are on the same page, and then change the page where you were before to the new url.

window.open(window.location.href)
window.location.href = new_url 
Diego Poveda
  • 110
  • 5
1

No, this is controlled by the browser.

Rusty Fausak
  • 7,355
  • 1
  • 27
  • 38
0

It can be done easily using javascript to intercept all clicks through a delegate function and then calling preventDefault() on the event. After that it is a matter of creating a pop-under window just like a nasty ad ;)

That said, don't do this unless you plan on pissing your users off :P

Martin Jespersen
  • 25,743
  • 8
  • 56
  • 68
-1

it opens on new tab or window target="_blank"

stay on your current tab or window target="_self"

-2

There isn't a way to currently control this.


If you want to remove the existing behaviour:

Is there a way to stay on the current window instead of going to the tab [when the link has target="_blank"] ?

Only if you do something like this first...

$('a[target="_blank"]').removeAttr('target');
alex
  • 479,566
  • 201
  • 878
  • 984
  • 5
    Not a joke, there are just (at least) 2 different interpretations to the question (and since this was accepted, alex's interpretation was surely not be completely off). **interpretation1:** Open link in new tab, but leave the focus on the page the user was on. **interpretation2:** Open the link in the current tab, when clicked (even though it has `target` set to `_blank`) - which is correctly answered here. – Levite Jun 17 '15 at 11:14