您的位置:首页 > Web前端 > JavaScript

window.close无效的js解决方案

2015-10-13 21:21 531 查看
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<script type="text/javascript">
alert("123");
window.close();
//window.open("about:blank","_self").close()
</script>
</head>

<body>
测试内容
</body>
</html>

点击“确定”,可以直接关闭该窗口。是这段代码在chrome和Firefox下面不会生效。

第一种解决方案:

1、针对chrome浏览器,可以在编写如下代码,使之生效:

window.open("about:blank","_self").close()   

   或者

   window.open("","_self").close() 
2、不是JS代码window.close()的问题,而是Firefox的配置问题,解决方法如下:

在Firefox地址栏里输入 about:config

在配置列表中找到 dom.allow_scripts_to_close_windows

点右键的选切换把上面的false修改为true即可。

第二种解决方案:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<script type="text/javascript">
//alert("123");
//window.close();
//window.open("about:blank","_self").close()
document.write("Name: " + navigator.appName);
var browserName=navigator.appName; //获取浏览器名称
if (browserName=="Netscape") {
function closeme() {
window.open('','_parent','');
window.close();
}
}else{
if (browserName=="Microsoft Internet Explorer") {
function closynoshowsme() {
window.opener = "test";
window.close();
}
}
}
</script>
</head>

<body>
<input type="button" value="close me 3" onclick="closeme();"/>
测试内容
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: