EnexoOnoma
EnexoOnoma

Reputation: 8834

How to stay on current window when the link opens in new tab?

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 ?

Upvotes: 44

Views: 104457

Answers (8)

Domenic Valenzuela
Domenic Valenzuela

Reputation: 1

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

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

Upvotes: -1

Jamie
Jamie

Reputation: 87

<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

Upvotes: 7

Diego Poveda
Diego Poveda

Reputation: 120

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 

Upvotes: 2

alex
alex

Reputation: 490657

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');

Upvotes: -2

Adel Bachene
Adel Bachene

Reputation: 994

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);
}

Upvotes: 6

Anita Whiteson
Anita Whiteson

Reputation: 29

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.

Upvotes: 1

Martin Jespersen
Martin Jespersen

Reputation: 26193

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

Upvotes: 0

Rusty Fausak
Rusty Fausak

Reputation: 7525

No, this is controlled by the browser.

Upvotes: 1

Related Questions