1

I'm using

target = "_blank"

to spawn a new tab when a link is clicked. However, the browser moves focus to that tab.

Is there a way to keep focus on the current tab?

Summary of Answer

Basically, just dispatch a current event that emulates a control click.

2 Answers2

2

You can use this function.

<button id="openLink" value="http://www.google.com">Open Link</button>

first you must add the event to the object

document.getElementById("openLink").addEventListener("click", openTab, false);

here is the function.

function openTab(){
    var a = document.createElement("a");
    a.href = document.getElementById("openLink").value;
    var evt = document.createEvent("MouseEvents");    
    evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, true, false, false, false, 0, null);
    a.dispatchEvent(evt);
}

I think I saw this question on the forum, if I find the link I'll stick

I made some changes, and i create a new jsfiddle -> http://jsfiddle.net/PXR8f/

Natalia
  • 470
  • 2
  • 5
  • 12
  • I posted this: http://stackoverflow.com/questions/10812628/open-a-new-tab-in-the-background for his answer. He said the fiddle was broken but that should work. I don't have time to test it out currently. – Jacob Morrison Apr 09 '13 at 18:42
  • this is the link. http://stackoverflow.com/questions/10812628/open-a-new-tab-in-the-background – Natalia Apr 09 '13 at 18:42
0

Try like below... It will help you....

<a href="http://www.google.com" onclick="window.open(this.href,'_self');window.open('#','_blank');">
    Click Here
 </a>
Pandian
  • 8,848
  • 2
  • 23
  • 33