简体   繁体   中英

open a new tab and don't lose focus

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....

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM