Scripts may close only the windows that were opened by it

时间:2023-01-18 02:23:39

关闭当前窗体报以下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();

成功关闭。