最近小编遇到一个需求:用户登录系统申请某个业务,当帐号等级低于L2级时弹框提示用户提升帐号等级,弹框上面有2个按钮“去提升”、“关闭”,前面一个按钮自然是去到提升页面,而后面按钮就是关闭当前页面,本以为很简单的功能,新版的谷歌运行时控制台提示警告:Scripts may close only the windows that were opened by it
意思是脚本只能关闭通过脚本打开的页面,当我们在浏览器地址栏输入URL打开页面,是不会通过window.close()关闭的
网上找了相关的解决方法最后验证都为成功:
function closePage(){
if(navigator.userAgent.indexOf("Firefox") != -1 || navigator.userAgent.indexOf("Chrome") != -1){
window.location.href = "about:blank";
window.close();
}else{
window.opener = null;
window.open("", "_self");
window.close();
}
}
小编试了如下8种方法,最后都以失败告终:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta charset="UTF-8"/>
<title>winOpen 测试</title>
</head>
<body>
<div>
winOpen 测试
<div>
<button type="button" onclick="openWindow()">open打开(self)</button>
<button type="button" onclick="openBlankWindow()">open打开(blank)</button>
<button type="button" onclick="closeWindow()">close关闭</button>
</div>
</div>
</body>
<script type="text/javascript">
function openWindow() {
console.log('openWindow 打开')
// window.open('./winopen.html')
window.open('./winopen.html', '_blank')
}
function openBlankWindow() {
console.log('openWindow 打开')
// window.open('./winopen.html')
window.open('./winopen.html', '_self')
}
function closeWindow() {
console.log('openWindow 关闭')
var userAgent = navigator.userAgent
if (userAgent.indexOf('Firefox') !== -1 || userAgent.indexOf('Chrome') !== -1) {
// 0、
window.close()
// 1、
// window.open('', '_self').close()
// 2、
// window.open('about:blank', '_self').close()
// 3、
// window.location.href = 'about:blank'
// window.close()
// 4、
// let winObj = window.open('about:blank')
// // console.log('winObj.location', winObj.location)
// winObj.close()
// 5、
// window.open('', '_self', '')
// window.close()
// 6、
// window.opener = null
// window.open('', '_self')
// window.close()
// 7、
// window.open(location, '_self').close()
} else {
window.opener = null
window.open('about:blank', '_self')
window.close()
}
}
</script>
</html>
有木有哪位小伙伴找个解决方法~~~
小编也有一些思路:
1、谷歌浏览器下:Ctrl + F4 关闭当前页签
2、调用谷歌底层关闭当前页签(太过复杂未实现)
来源:CSDN
作者:欣怡
链接:https://blog.csdn.net/niuch1029291561/article/details/104009031