UPDATE: By version 41 of Google Chrome, initMouseEvent
seemed to have a changed behavior, thanks to @Daniel Andersson
this can be done by simulating ctrl
+ click
(or any other key/event combinations that open a background tab) on a dynamically generated a
element with its href
attribute set to the desired url
In action: 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);
}
tested only on chrome
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…