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 小卓 閱讀(2301) 評論(0)  編輯  收藏 所屬分類: html and js

          主站蜘蛛池模板: 淮南市| 武清区| 白银市| 莱芜市| 甘南县| 黄陵县| 镇赉县| 红安县| 仁布县| 鲁甸县| 静宁县| 兰州市| 鄂温| 镇江市| 遂宁市| 竹山县| 长丰县| 芦溪县| 卢湾区| 伊吾县| 从江县| 长沙市| 外汇| 南郑县| 万安县| 楚雄市| 东山县| 富宁县| 铁岭县| 中宁县| 灵璧县| 繁峙县| 乐平市| 永仁县| 大港区| 菏泽市| 濮阳县| 新乡县| 万年县| 沂南县| 栖霞市|