您的位置:首页 > 运维架构

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

2015-06-26 15:26 639 查看
由于在脚本中使用了 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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: