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

Viewed 69581

I have a window I'm opening with a Javascript function:

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

I need it that once the new window opens that the focus returns to the original window. How can I do that? And where do I put the code - in the new window, or the old one? Thanks!

4 Answers

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.

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.

Related