How to open a new background tab and redirect the current tab?

Asked

Viewed 1,258 times

0

I would like when clicking 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.

This is possible?

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

<a href="https://answall.com" target="_blank">Visite o Stackoverflow!</a>

  • 3

    Can you describe the goal? It is not very clear why to open another page without directing the user. Why the user would click on a link like this?

  • It seems the procedure that these boring ad sites do, click to see the video and open 500 pages in the background.

  • @Knautiluz, that :) kkk

  • 1

    Now I understand (I think). You have no control over this, who decides whether the new tab will open in the background or not is the browser and user settings.

  • So@bfavaretto, adult content sites do this, so I believe it’s possible, I think

  • 2

    They have new windows opened, but the browser defines whether it will open as a window, as a tab, and whether it will focus or keep in the background.

Show 2 more comments

5 answers

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('/');
}

Open one on the same tab and an external tab

HTML:

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

JS:

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

3


Apparently it is not possible to open a tab in the background, I found this reply, 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://answall.com" onclick="openNewBackgroundTab('https://www.google.com');">Redireiciona para a Home</a>

However didn’t work, I believe Chrome has removed this capability.

I think it is not possible at all to do this, because that would be to control the machine/decision of the end user and actually put yourself in its place, you would like it to be opened?

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

3

It’s very simple, just add Javascript:

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

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

Good to do what you want there are some possibilities. 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.

  • Almost that, but I didn’t want the page that opened the browser to redirect me, I wanted to continue on the page that I was on, and say, just open the other page in another tab or new window, if you know what I mean :)

  • But @David, in the question you said you wanted to open two Urls!

  • @bfavaretto, I agree, but I wanted you to do this tbm, I edit the question?

  • I understood what you want. You want to simulate Ctrl + click. is in the current tab and opens another without changing tab.

  • 1

    It’s confusing, the two things at once do not give. Or you want to open an address or two...

  • issooooo @Bruno

  • @bfavaretto, 2 addresses

  • I updated the answer, there’s a link that will help you.

  • Exactly that @Bruno, thank you.

  • Quiet man, I’m glad I helped.

  • 1

    But what changed in his solution from when it was "almost that" to now that is "exactly that", @David?

Show 6 more comments

1

Try that code:

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

-2

Something like:

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

And you:

$('.meusSites').click(function(e) {
e.preventDefault();
window.open($(this).data('site1'));
location.href=$(this).data('site2')
});
  • Didn’t work :/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.