views:

91

answers:

2

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!

+5  A: 

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.

DaRKoN_
I tried using the blur() thing, but it didn't work so well in Firefox, so I guess that should work better - I just wasn't sure how to refer back to the original window.I also saw something about window.opener.focus() = does anyone know how I would use that?
IsaacL
Tested with window.opener from http://amar-desh.net/stackoverflow/openwindow.aspx
Hoque
A: 

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.

Thanks.

Hoque
It seems like that doesn't work well in Firefox though...
IsaacL
It works for me in the FireFox,what type of problem have you obeserved.Thanks.
Hoque
I have tested both in IE and FireFox. I did not see any problem during my test.Here is a test page, http://amar-desh.net/stackoverflow/openwindow.aspx
Hoque
You're right - not sure that code will work in Chrome though.I'll try it - thanks a lot!!
IsaacL