解决各种浏览器关闭当前窗口的问题

    xiaoxiao2023-03-25  4

    问题产生原因:1)直接写window.close进行关闭

                             2)写了这些,对chrome 及Firefox还是是无效

    window.opener = null; window.open("", "_self"); window.close();

    报的错误:Scripts may close only the windows that were opened by it.

    解决方案:基于安全机制的考虑,只有通过js代码 打开的窗口才能关闭,例如window.open。没有父窗口的话,2)这种写法也是不行的

                       如下写法可以解决该问题(兼容所有浏览器): 即设置当前页为空白页。

    <script> function closePage() { var userAgent = navigator.userAgent; if (userAgent.indexOf("Firefox") != -1 || userAgent.indexOf("Chrome") != -1) { window.location.href = "about:blank"; } else { window.opener = null; window.open("", "_self"); window.close(); } } </script>

    

    转载请注明原文地址: https://ju.6miu.com/read-1203611.html
    最新回复(0)