js关闭页面提示Scripts may close only the windows that were opened by it

在页面中触发window.close(),页面没有按照预期那样关闭,控制台提示警告:Scripts may close only the windows that were opened by it

意思是脚本只能关闭通过脚本打开的页面,当我们在浏览器地址栏输入URL打开页面,是不会通过window.close()关闭的

 

网上找到的解决方法,亲测有效:

function closePage(){
    if(navigator.userAgent.indexOf("Firefox") != -1 || navigator.userAgent.indexOf("Chrome") != -1){
        window.location.href = "about:blank";
        window.close();
    }else{
        window.opener = null;
        window.open("", "_self");
        window.close();
    }
}

 

你可能感兴趣的:(js)