0

Let's have a simple code like this:

$(e).append(
  $("<a>").attr('href', url)
          .attr('target', '_blank')
          .text(text)
);

How can I prevent document losing a focus when new tab is opened?

I want a new tab to be opened in background without switching a focus into it.

Ωmega
  • 42,614
  • 34
  • 134
  • 203

1 Answers1

0

Try using this javascript by using this question's accepted answer:

$('a[target="_blank"]').removeAttr('target');

Using the same question's most upvoted answer, try this aswell:

<a href="your_url" onclick="window.open('#','_blank');window.open(this.href,'_self');">

Using this question's accepted answer, try this:

Fiddle.

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

Here the tenth parameter sets the ctrl key to be true. Then that just acts like a ctrl + click and then that's opened in an another window without losing focus.

Community
  • 1
  • 1
Mohammad Areeb Siddiqui
  • 9,795
  • 14
  • 71
  • 113