如果没有父窗口,JS中
window.opener = null;
window.open("", "_self");
window.close();
对chrome 及Firefox是无效的,那么针对这种情况该怎么办呢。
工具/原料
-
chrome,Firefox浏览器
方法/步骤
-
JS 关闭网页,之前通过以上方式,可以解决Firefox chrome等浏览器不兼容的问题,但是随着浏览器版本升级,现在无效了。会提示:
Scripts may close only the windows that were opened by it.
-
那么如下写法可以解决该问题: 即,如果是以下浏览器,则设置当前页为空白页。
$("#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();
}});
});