Skip to content

Knowing whether window.close() will show a security warning

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.

Answer

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?