Reputation:
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.
Upvotes: 1
Views: 1426
Reputation: 470
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/
Upvotes: 2
Reputation: 9126
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>
Upvotes: 0