table

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

方法一:

js 代码
  1. function CloseWin() //这个不会提示是否关闭浏览器   
  2. {   
  3. window.opener=null;   
  4. //window.opener=top;   
  5. window.open("","_self");   
  6. window.close();   
  7. }   
方法二:
open.html
js 代码
  1. function open_complex_self() {   
  2.   var obj_window = window.open('close.html', '_self');   
  3.   obj_window.opener = window;   
  4.   obj_window.focus();   
  5.  }   
close.html
js 代码
  1. window.close();  
另附:
//普通带提示关闭
function closeie(){ window.close(); }
//关闭IE6不提示 function closeie6(){ window.opener=null; window.close(); }
//关闭IE7不提示 function closeie7(){ window.open('','_top'); window.top.close(); }


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

<a href="javascript:self.close()">关闭窗口</a>

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

<a href="javascript:alert('Hello World')">弹出窗口</a>

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

改成了如下代码

<!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="#" onclick="windowClose()">js函数关闭窗口</a>
<body>
</body>
</html>


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

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

1.open.html

<script type="text/javascript">
<!--
    function openWindow(){
        window.open("new.html","newWindow","width=200,height=100,toolbar=no");
    }
//-->
</script>
<a href="#" onclick="openWindow()">open函数打开新窗口</a><br />
<a href="new.html" target="_blank">超级链接在新窗口中打开新页面</a><br />
<a href="new.html" target="_parent">超级链接在父窗口中打开新页面</a>

2.new.html

<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打开的窗口时会弹出一个对话框询问用户。有时候我

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

<script   language="javascript"   type="text/javascript"> 
  
  function   closeWindow()   { 
  
  window.open('','_parent',''); 
  
  window.close(); 
  
  } 
  
  </script>   
  <a   href="javascript:closeWindow();">Close   Window</a>


参考文章:1.http://hi.baidu.com/suen_%CB%EF/blog/item/bedca57f8932480d28388a49.html
               2.http://blog.csdn.net/a9529lty/archive/2008/11/22/3351539.aspx


文章出处:DIY部落(http://www.diybl.com/course/1_web/javascript/jsjs/20090318/162531.html)

posted on 2009-04-24 13:35 小卓 阅读(2289) 评论(0)  编辑  收藏 所属分类: html and js


只有注册用户登录后才能发表评论。


网站导航: