开发者

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

开发者 https://www.devze.com 2023-04-07 01:24 出处:网络
When a user clicks on a link <a href=\"http:开发者_如何转开发//www.stackoverflow.com\" target=\"_blank\">click</a>

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 ?


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


<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


No, this is controlled by the browser.


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.


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 


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


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

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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号