A snippet to open links in a background tab. The idea is to trigger a mouse click event with control key pressed set as true, to open the clicked link in a background tab.
<a class="bgLink" href="http://www.example.com">Example</a>
<a class="bgLink" href="http://foo.example.com">Example Foo</a>
// js
window.onload = function () {
//event listeners
var a = document.querySelectorAll(".bgLink");
if (a != null) {
var len = a.length, i=0;
if (len > 0) {
for (i = 0; i < len; i++) {
a[i].addEventListener("click", openNewBackgroundTab, false);
}
}
}
// event handlers
// open links in background tabs
function openNewBackgroundTab(e){
var a = document.createElement("a"), evt;
a.href = this.getAttribute('href');
evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, true, false, false, false, 0, null);
a.dispatchEvent(evt);
e.preventDefault();
}
};
Reference: initMouseEvent
Update: Stopped working with Google Chrome dev release 41.0.2224.0
after 2014-11-20.