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

来源:互联网 发布:时空数据可视化 编辑:程序博客网 时间:2024/06/05 06:03

问题产生原因: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>







0 0