在页面中触发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();
    }
}

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐