可以打开,但不能关闭
加了句
window.location.href='about:blank'; 又可以了,请问什么原理
代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>打开/关闭页面</title>
</head>
<body>
<input type="button" value="关闭本页面" id="close">
<input type="button" value="打开慕课网" id="open">
<script type="text/javascript">
//补充代码
var
btnOpen = document.getElementById('open'),
btnClose = document.getElementById('close');
btnOpen.onclick = function(){
window.open('http://www.imooc.com');
};
btnClose.onclick = function(){
window.location.href='about:blank';
// window.open('','_self','');
window.close();
}
</script>
</body>
</html>
正在回答 回答被采纳积分+1
同学你好,老师使用谷歌、火狐浏览器是可以关闭该html页面的。请问同学是使用什么浏览器测试的呢?
另,“Scripts may close only the windows that were opened by it ”不是报错,而是警告,意思是“脚本只能关闭通过脚本打开的页面,当我们在浏览器地址栏输入URL打开页面,是不会通过window.close()关闭的”这可能是某些浏览器自身的限制问题。
建议同学可以在谷歌浏览器中测试,并通过(以sublime编辑器为例)在编辑器中右击->在浏览器中打开的方式,去打开html页面测试。
祝学习愉快~
网上答案看都看不懂,粘贴过来还是不行。
例:
try {
window = window.opener ; //父源窗口
var win = window.open("","_self");
win.close();
//frame的时候
top.close();
} catch (e) {
console.log(e);
}
报错:Scripts may close only the windows that were opened by it
但代码修改后不能关闭整个浏览器页面,知识修改了location并不代表真正关闭,如何解决
相似问题
登录后可查看更多问答,登录/注册
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星