I need to open a new window in the background with JavaScript, and make sure the original is still focused

This is known as a 'pop-under' (and is generally frowned upon... but I digress).. It should give you plenty to google about

You probably want to do something like:

var popup = window.open(...);
popup.blur();
window.focus();

Which should set the focus back to the original window (untested - pinched from google). Some browsers might block this technique.


After calling window.open, you may try to use

window.resizeTo(0,0); 
window.moveTo(0,window.screen.availHeight+10);

this way can not really open window in background, but works in similar way. Chrome works fine, did not try other browser.


If Albert's solution doesn't work for you and you actually want the window visible, but to be opened behind the current window, you can try opening a new tab in the opener window and closing it right away, this will bring the focus back to the opener window.

window.open('link.html','','width=,height=,resizable=no');  
window.open().close();

However, I believe whether the second window opens in a tab or a new window depends on your browser settings.

Please don't use "pop-unders" for evil.


You can use either "blur" or "focus" to do that required action.

"blur"

function newwindow()  
{  
    var myChild= window.open('link.html','','width=,height=,resizable=no');  
    myChild.blur();
} 

"focus"

function newwindow()  
{  
    window.open('link.html','','width=,height=,resizable=no');  
    window.focus();
} 

Put the code in your parentWindow (i.e. the window in which you are now)

Both will work.