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

Answers


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.


Need Your Help

How to find locations of an element in a matrix

matlab

I have a matrix A, that some elements of this matrix is repeated. I need to find locations of an

MFC, How do I change the capiton of TabCtrl

mfc caption ctabctrl

I know the InsertItem is for add new tab in tabctrl, but I need change the caption of tab after create, I use keyword tabctrl and caption to search the old article ,but no relation problem to solve...