【问题标题】:self.close() not working for mozillaself.close() 不适用于 Mozilla
【发布时间】:2012-07-17 23:29:52
【问题描述】:

self.close() 在 IE 中运行良好,但在 Mozilla 中运行良好。有谁知道这是什么问题,我该如何解决?

【问题讨论】:

    标签: javascript


    【解决方案1】:

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

    这个方法只允许被脚本使用 window.open 方法打开的窗口调用。如果窗口不是由脚本打开的,JavaScript 控制台中会出现以下错误:脚本可能不会关闭不是由脚本打开的窗口。

    【讨论】:

      【解决方案2】:

      尝试改用window.close()

      【讨论】:

        【解决方案3】:

        查看我对this other question 的回复。您应该能够轻松地将其从 ASP.NET 调整为纯 HTML。

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

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

        现在你的窗口已经被js打开了,你可以用js关闭它了! :) 关闭.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>
        

        【讨论】:

        • 你会注意到我使用了“window.timeout”,这样如果窗口在 2000 毫秒后没有自动关闭,它将重定向到 default.aspx。这样你用过的就不会挂在窗户没有关闭的情况下。例如在 IE7 中,它会提示用户,他们可以拒绝关闭窗口。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-02-10
        • 2012-08-09
        • 2012-03-25
        • 2012-12-12
        • 2017-08-07
        • 2019-12-28
        • 1970-01-01
        相关资源
        最近更新 更多