开发者

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 betwe开发者_高级运维en 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.


Try comparing window.opener and window.self


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?


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


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().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜