How to open a new tab in the background and redirect the current tab? [closed]

0

I would like to click on the link to open a new tab (but in the background - I want the focus tab to remain the same) and the current tab to be redirected.

Is this possible?

With the code below, a new tab opens only and the focus is changed for it.

<a href="https://pt.stackoverflow.com" target="_blank">Visite o Stackoverflow!</a>
    
asked by anonymous 01.08.2017 / 21:16

5 answers

3

Apparently it is not possible to open a background tab, I found this response , the test:

function openNewBackgroundTab(url)
{
    var a = document.createElement("a");
    a.href = url;
    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="https://pt.stackoverflow.com" onclick="openNewBackgroundTab('https://www.google.com.br');">Redireiciona para a Home</a>

However did not work , I believe Chrome has removed this capability.

I do not think it's possible to do this at all, because that would be to control the end user's machine / decision and actually put yourself in his place, would you like it to be open?

The control of where the page will open should be the user's decision I believe, maybe I should change the approach.

    
01.08.2017 / 22:14
4

You can do this using javascript:

Open two separate tabs

HTML:

<a href="#" onclick="abrir_pagina();">Abre duas páginas</a>

JS:

function abrir_pagina() {       
    window.open('http://google.com');
    window.open('https://pt.stackoverflow.com/');
}

Open one on the same flap and one external flap

HTML:

<a href="https://pt.stackoverflow.com/" onclick="abrir_pagina();">Abre duas páginas</a>

JS:

function abrir_pagina() {       
    window.open('http://google.com');
}
    
01.08.2017 / 21:20
3

It's quite simple, just add Javascript:

    <a href="https://pt.stackoverflow.com" target="_blank" onclick = "window.location.href = 'http://www.google.com';">Visite o Stackoverflow!</a>

It worked, one opens on the other page and the other one updates.

Good to do what you want there is some possibility. I recommend seeing How to open a page in a new tab without leaving the current page .

But note that this will depend on the browser and that this can be interpreted as a practice of disrespect to the site user.

    
01.08.2017 / 21:25
1

Try this code:

$('a.seulink').click(function(e) {
    e.preventDefault();
    window.location= 'http://pagina1.com';
    window.open('http://pagina2.com');
});
    
01.08.2017 / 21:27
-2

Something like:

<a href="javascript:void(0)" class="meusSites" data-site1="http://google.com" data-site2="http://youtube.com">2 url</a>

And the js:

$('.meusSites').click(function(e) {
e.preventDefault();
window.open($(this).data('site1'));
location.href=$(this).data('site2')
});
    
01.08.2017 / 21:28