javascript关闭窗口(兼容firefox,IE)

javascript关闭窗口,可以用下面简单的代码:

Html代码 复制代码
  1. <a href="javascript:self.close()">关闭窗口</a>    
<a href="javascript:self.close()">关闭窗口</a>  


我在IE7下测试通过,但是firefox3.0却不行。
难道firefox不支持在href中直接写JavaScript?于是改成下面的样子:

Html代码 复制代码
  1. <a href="javascript:alert('Hello World')">弹出窗口</a>    
<a href="javascript:alert('Hello World')">弹出窗口</a>  


这次IE7和firefox下测试都通过。那就不是href中直接写JavaScript的原因了。
继续测试firefox怎么关闭自身窗口

改成了如下代码

Html代码 复制代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"       
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">     
  3. <html xmlns="http://www.w3.org/1999/xhtml">     
  4. <head>     
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />     
  6. <script type="text/javascript">     
  7. <!--      
  8. function windowClose(){      
  9. //self.close();      
  10. window.close();      
  11. }      
  12. //-->     
  13. </script>     
  14. <title>js测试</title>     
  15. </head>     
  16. <a href="javascript:self.close()">关闭窗口</a><br />     
  17. <a href="javascript:alert('Hello World')">弹出窗口</a><br />       
  18. <a href="#" onclick="windowClose()">js函数关闭窗口</a>     
  19. <body>     
  20. </body>     
  21. </html>    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<script type="text/javascript">  
<!--   
function windowClose(){   
//self.close();   
window.close();   
}   
//-->  
</script>  
<title>js测试</title>  
</head>  
<a href="javascript:self.close()">关闭窗口</a><br />  
<a href="javascript:alert('Hello World')">弹出窗口</a><br />    
<a href="#" οnclick="windowClose()">js函数关闭窗口</a>  
<body>  
</body>  
</html>  


还是不能关闭窗口。难道firefox不支持window的close属性?
那window对象的close方法能不能关闭open方法打开的窗口呢?

写下面两个html文件放在同一个文件夹下

Open.html代码 复制代码
  1. <script type="text/javascript">      
  2. <!--      
  3. function openWindow(){      
  4. window.open("new.html","newWindow","width=200,height=100,toolbar=no");      
  5. }      
  6. //-->      
  7. </script>      
  8. <a href="#" οnclick="openWindow()">open函数打开新窗口</a><br />      
  9. <a href="new.html" target="_blank">超级链接在新窗口中打开新页面</a><br />      
  10. <a href="new.html" target="_parent">超级链接在父窗口中打开新页面</a>    
<script type="text/javascript">   
<!--   
function openWindow(){   
window.open("new.html","newWindow","width=200,height=100,toolbar=no");   
}   
//-->   
</script>   
<a href="#" οnclick="openWindow()">open函数打开新窗口</a><br />   
<a href="new.html" target="_blank">超级链接在新窗口中打开新页面</a><br />   
<a href="new.html" target="_parent">超级链接在父窗口中打开新页面</a>  

 

New.html代码 复制代码
  1. <a href="javascript:window.close()">关闭窗口</a>      
  2. <a href="javascript:self.close()">关闭窗口</a>   
<a href="javascript:window.close()">关闭窗口</a>   
<a href="javascript:self.close()">关闭窗口</a> 


用open方法和在"_blank"打开的可以在新窗口中关闭,而在"_parent"中打开的在firefox中还是关闭不



因此在firefox里用window的close方法时要注意他和IE不同的地方:在父窗口打开的页面是不能用close

方法关闭的。

然后去google搜了一下:之所以window.close在firefox不能使用,是因为firefox默认不能关闭用户打

开的网页,我们可以这样设置firefox:

打开firefox,在地址栏输入about:config

找到dom.allow_scripts_to_close_windows这项并改为true。

现在知道为什么了吧。那篇文章还有一段不错的内容,摘录如下:

众所周知,在javascript中window.close()是用来关闭窗口的,而且ie和firefox都是支持的。为了实现

用户对浏览器的绝对控制,ie中用close关闭非open打开的窗口时会弹出一个对话框询问用户。有时候我

们不希望再这样哆嗦,但是怎么去掉这个框呢,用下面的代码就可以了

Html代码 复制代码
  1. <script language="javascript" type="text/javascript">       
  2.      
  3. function closeWindow() {       
  4.      
  5. window.open('','_parent','');       
  6.      
  7. window.close();       
  8.      
  9. }       
  10.      
  11. </script>       
  12. <a href="javascript:closeWindow();">Close Window</a>    
<script language="javascript" type="text/javascript">    
  
function closeWindow() {    
  
window.open('','_parent','');    
  
window.close();    
  
}    
  
</script>    
<a href="javascript:closeWindow();">Close Window</a>  

     转载:http://wangyu.iteye.com/blog/490831

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值