javascript - using click() function to open a link but keeping focus of the current window -


i have couple of links on page , i'm using javascript function "click()" open links @ once, don't want focus move new tab :

for example, i'll use code :

document.getelementbyid("link1").click(); 

but want keep focus of current tab.

you can simulate ctrl + click

javascript

document.getelementbyid("test").addeventlistener("click", openbackgroundtab, false);  function openbackgroundtab(){     var = document.createelement("a");     a.href = document.getelementbyid("test").getattribute("href");     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);     return false; } 

html

<a id="test" href="http://www.google.com/">open google</a> 

working demo


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

java.util.scanner - How to read and add only numbers to array from a text file -

iphone - Three second countdown in cocos2d -