场景:
在实际业务场景中,我们可能会遇到,想要使用js来关闭当前窗口;
通常我们是使用 window.close() 方法来进行操作;
但有时候我们会遇到无法关闭的情况,并且控制台还给出以下警告:
Scripts may close only the windows that were opened by them.
查看了相关的原因是因为,浏览器有相关规定:
只有通过 window.open(url) 打开的窗口,才能够由 window.close() 关闭,为的是阻止恶意的脚本终止用户的浏览器;
我们可能会遇到以下问题不能 使用 window.close 关闭:
1、a标签打开;
IE将会弹出确认窗口;
window.opener = null;//禁止某些浏览器的一些弹窗
window.open('','_self');
window.close();
2、window.open('xxx','_self');
IE支持该方法;
chrome、firefox测试后暂不支持;
3、window.location.href;
IE将会弹出确认窗口;
正确的使用方法是:
1、使用 window.open(url) 打开窗口;
2、使用 window.close() 来关闭窗口;
网友评论