window.close()提示 "Scripts may close only the windows that were opened by it"

来源:互联网 发布:免费加入团队软件 编辑:程序博客网 时间:2024/06/06 05:46

由于在脚本中使用了 window.close(), 当前非弹出窗口在最新版本的chrome和firefox里总是不能关闭,而在 IE中是可以关闭的 。
在console中弹出提示”Scripts may close only the windows that were opened by it”

首先,什么是非弹出窗口呢?
非弹出窗口,即是指(opener=null 及 非window.open()打开的窗口,比如URL直接输入的浏览器窗体, 或由其它程序调用产生的浏览器窗口)。

其次,window.close() 怎么理解呢?
可参考 https://developer.mozilla.org/en-US/docs/Web/API/window.close

在某些实际应用中,window.close() and self.close() 是不能关闭非弹出窗口(opener=null及非window.open()打开的窗口)。

以下代码也只是保证了在ie下不再出现确认对话框, 对chrome和ff不能关闭是没有任何作用的

<script type="text/javascript">function closeWP() { var Browser = navigator.appName; var indexB = Browser.indexOf('Explorer'); if (indexB > 0) {    var indexV = navigator.userAgent.indexOf('MSIE') + 5;    var Version = navigator.userAgent.substring(indexV, indexV + 1);    if (Version >= 7) {        window.open('', '_self', '');        window.close();    }    else if (Version == 6) {        window.opener = null;        window.close();    }    else {        window.opener = '';        window.close();    } }else {    window.close(); }}</script>

https://developer.mozilla.org/en-US/docs/Web/API/window.close
https://productforums.google.com/forum/#!topic/chrome/GjsCrvPYGlA
http://stackoverflow.com/questions/19761241/window-close-and-self-close-do-not-close-the-window-in-chrome

最后结论:
在新弹出的标签页中去掉”关闭页面”的按钮, 很多网站包括百度等都规避了window.close()

0 0
原创粉丝点击