chrome JS关闭当前页无效问题

如果没有父窗口,JS中 

window.opener = null;

window.open("", "_self");

window.close(); 

对chrome 及Firefox是无效的,那么针对这种情况该怎么办呢。

工具/原料

  • chrome,Firefox浏览器

方法/步骤

  1. 1

    JS 关闭网页,之前通过以上方式,可以解决Firefox chrome等浏览器不兼容的问题,但是随着浏览器版本升级,现在无效了。会提示:

    Scripts may close only the windows that were opened by it.

  2. 2

    那么如下写法可以解决该问题: 即,如果是以下浏览器,则设置当前页为空白页。

    $("#closeBtn").click(function(){

    var userAgent = navigator.userAgent;

    if (userAgent.indexOf("Firefox") != -1 || userAgent.indexOf("Chrome") !=-1) {

       window.location.href="about:blank";

    } else {

       window.opener = null;

       window.open("", "_self");

       window.close();

    }});

    });

原文地址:https://www.cnblogs.com/wdlhao/p/5329730.html