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

由于在脚本中使用了 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值