How to open a page in a new tab without leaving the current page

1

What I want is something that works like target="_blank" , but I do not want to leave the page where the link was clicked.

That is, clicking on the link would seemingly nothing change, keeping the current page view, however, a new tab would be opened.

What code could you use to do this?

    
asked by anonymous 15.11.2014 / 00:34

2 answers

3

New Tab x New Window

First, just to clarify: about opening in a "new tab". If you want to open only on a new tab rather than a new window, this is not currently possible. CSS3 sets the target-new property to deal with this, but it has not been deployed in any browser so far.

Ok, now about the main theme:

Open link in the background

You can do a workaround emulating Ctrl + Click :

<a href="http://pt.stackoverflow.com/questions/40573" id="link">Link para esta questão</a>
<button onclick="simulateClick(true, false, false);">Simulate Ctrl+Click</button>
var simulateClick = function (ctrl, shift, isMiddle) {
    var evt = document.createEvent('MouseEvents');
    evt.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, ctrl, false, shift, false, isMiddle ? 1 : 0, null );
    document.getElementById('link').dispatchEvent(evt);
}

View a similar jsFiddle

However,

This is a browser behavior , user-configured . And should be like this and only thus. Rightly so, we do not want to open the possibility to open links in the background without noticing while browsing the web! This is a user option!

More than that, even target="blank" is only supported on certain occasions: when the user has something in progress that should not be lost with a local page redirect. An open link while playing a video, or an unsaved draft of an email in a web mail client.

The idea of all this is that we should not change the browser's natural behavior. If the user wants to open the link in the window itself, leave it. If he wants to open another, leave it. If he wants to open it on a background flap, leave it. But if he wants to open it first, leave it too.

The focus of the web application on this should be on providing the best user experience through a good layout and a smooth flow without limiting the user to choices he can in another application. This approach may even irritate the user.

    
15.11.2014 / 01:41
0

You'll need a code like this:

function openNewBackgroundTab(){
    var a = document.createElement("a");
    a.href = window.location.pathname;
    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);
}

var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
if(!is_chrome)
{
    var url = window.location.pathname;
    var win = window.open(url, '_blank');
} 
else 
{
    openNewBackgroundTab();
}

Source: link

    
15.11.2014 / 00:59