self.close() working fine in IE but not in Mozilla, anyone know what to coused this issue? and how can I solve it?
+6
A:
Did you open the window using window.open
? According to the docs on window.close:
This method is only allowed to be called for windows that were opened by a script using the window.open method. If the window was not opened by a script, the following error appears in the JavaScript Console: Scripts may not close windows that were not opened by script.
Paolo Bergantino
2009-02-18 00:32:32
A:
I tried both self.close() and window.close() but it seem the same and not working in Mozilla
Jin Yong
2009-02-18 00:40:08
This should be added as a comment to the answer you're responding to, not as an answer to the question. This isn't a messageboard!
Rex M
2009-02-18 00:41:12
What Rex said. Also, read Paolo's answer.
Shog9
2009-02-18 00:46:53
A:
See my answer to this other question. You should be able to easily adapt it from ASP.NET to plain HTML.
Basically, since mozilla will only let you close a window that was opened by js, you can open a new window and target _self:
window.open('close.html', '_self', null);
now your window was opened by js and you can close it with js! :) close.html:
<html><head>
<title></title>
<script language="javascript" type="text/javascript">
var redirectTimerId = 0;
function closeWindow()
{
window.opener = top;
redirectTimerId = window.setTimeout('redirect()', 2000);
window.close();
}
function stopRedirect()
{
window.clearTimeout(redirectTimerId);
}
function redirect()
{
window.location = 'default.aspx';
}
</script>
</head>
<body onload="closeWindow()" onunload="stopRedirect()" style="">
<center><h1>Please Wait...</h1></center>
</body></html>
rally25rs
2009-02-18 00:55:15
you'll notice i used a 'window.timeout' so that if the window doesn't self-close after 2000ms, it will redirect to default.aspx. That way your used isnt left hanging incase the window doesn't close. for example in IE7 it will prompt the user and they can say 'no' to letting the window close.
rally25rs
2009-02-18 00:57:36