self.close()不适用于mozilla

时间:2009-02-18 00:30:27

标签: javascript

self.close()在IE中正常工作但在Mozilla中没有。有谁知道这是什么问题,我该如何解决?

3 个答案:

答案 0 :(得分:9)

您是否使用window.open打开了窗口? According to the docs on window.close

  

只允许对使用window.open方法由脚本打开的窗口调用此方法。如果脚本未打开窗口,则JavaScript控制台中将显示以下错误:脚本可能无法关闭脚本未打开的窗口。

答案 1 :(得分:0)

请尝试使用window.close()

答案 2 :(得分:0)

请参阅我对this other question的回答。您应该能够轻松地将其从ASP.NET改为纯HTML。

基本上,由于mozilla只允许你关闭js打开的窗口,你可以打开一个新窗口并定位_self:

window.open('close.html', '_self', null);

现在你的窗口被js打开了,你可以用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>
相关问题