self.close() is working fine in IE but not in Mozilla. Does anyone know what is the issue and how can I solve it?
Asked
Active
Viewed 1.2k times
3 Answers
9
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
- 480,997
- 81
- 517
- 436
0
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>

Community
- 1
- 1

CodingWithSpike
- 42,906
- 18
- 101
- 138
-
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. – CodingWithSpike Feb 18 '09 at 00:57