Scripts may close only the windows that were opened by it

本文介绍了一种在JavaScript中正确关闭浏览器窗口的方法,并提供了一个具体的示例。当尝试关闭非自身脚本打开的窗口时,会遇到限制。文章给出了解决方案,即先将窗口地址指向空白页再关闭。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值