Scripts may close only the windows that were opened by it

关闭当前窗体报以下js错误:

Scripts may close only the windows that were opened by it

(脚本只能关闭由它打开的窗口)


使用场景,在js中关闭页面的js,如下:

window.opener = null;
window.open('', '_self');
window.close();

未能通过。

关闭页面的代码写在一个ajax返回的结果中的,如下:

$.ajax({
                type: 'post',
                url: "url",
                success: function (data) {
                    if (data.isOut) {
                        window.opener = null;
                        window.open('', '_self');
                        window.close();
                    }
                    else {
                        alert(data.msg);
                    }
                },
                error: function (err) {
                    alert("异常:" + err);
                }
            })

解决办法:

window.location.href="about:blank";
window.close();

成功关闭。

原文地址:https://www.cnblogs.com/senyier/p/8135916.html