Reputation: 1183
Hello I'm Using javascript, I want to open a new page in a different tab, but remain focused on the current tab. I know I can do it like this:
JS
document.getElementById("test").addEventListener("click", openNewBackgroundTab, false);
function openNewBackgroundTab(){
var a = document.createElement("a");
a.href = "http://www.3nytechnology.com/";
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);
}
HTML
<a id="test" href="http://www.stupidcodes.com" target="_blank" >Open Google</a>
but this code is not working in FireFox 26.0
can any one help me to solve this problem....
Upvotes: 1
Views: 4030
Reputation: 1085
This can be done by simulating CTRL + click event
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);
}
Tested only on chrome.
Upvotes: 1