views:

167

answers:

4

In IE7, a child window opened with window.open can close itself using window.close(), but a window opened with <a href=... target=_blank> will show a security warning if the child window tries to close itself.

In my application, I don't know how my child window is opened, and I need to know (in the child window javascript code) whether I can use the window.close() or not. Is there a way? Another way to ask the question is - is there a way in IE to differentiate between a window opened via window.open vs a window opened via target=_blank.

I tried checking window.opener but in both cases there is a value there, so this does not allow me to differentiate between the two cases.

A: 

Just a blind shot, but you can try removing the onunload event in the window, in case it is there.

Victor
You're a genius! I would NEVER have thought that it was THAT simple to remove the warning. It works in IE8 at least. Will check it in IE7 too.
giltayar
Nope. My bad. I probably HOPED it works, but it doesn't. BTW, the onunload event occurs AFTER the window.close warning happens.
giltayar
A: 

If you have control over the window.open events, you could give the new window a name (2nd parameter I think). You can then check for that name before applying window.close().

Pekka
Yes, that works, except that I don't have control over the opening!
giltayar
+1  A: 

Try comparing window.opener and window.self

jerjer
Unfortunately, the window.opener in both cases is the correct one, i.e. the parent window that opened this child window.
giltayar
A: 

Source: Close window without the prompt message in IE7

This is how to avoid the prompt according to the page above:

function WinClose(){window.open('','_self','');window.close();}

<a href="#" onclick="WinClose();return false;">Close</a>

Is this a possible approach for your page?

Christofer
This works. Thanks. I now have other problems that are IE8 specific, but I guess I'll post it in another question.
giltayar