相容所有瀏覽器的視窗關閉方法

來源:互聯網
上載者:User

標籤:style   blog   color   os   java   io   ar   div   cti   

平時在做項目的時候會碰到在頁面點擊文字直接退出瀏覽器視窗的效果!但是瀏覽器在實現關閉效果是不一致的!

下面的demo介紹的這個demo可以正常關閉ie,chrome等主流瀏覽器!但是Firefox的實現是通過開啟一個空頁面來實現的!如果要實現和ie一樣關閉的效果,需要開啟相應的許可權!

按照如下所示操作:

開啟firefox,在地址欄輸入about:config
找到dom.allow_scripts_to_close_windows這項並改為true。

但是做出來的頁面是給使用者體驗的,這種需要使用者更改設定的方式往往是不可取的!所以這裡我就通過開啟空頁面來代替!

demo如下:

<!DOCTYPE HTML><html lang="en-US"><head>    <meta charset="UTF-8"/>    <title>window_close</title></head><body><a href="javascript:;" onclick="CloseWebPage()">關閉視窗</a></body></html><script type="text/javascript">        function CloseWebPage() {
//判斷是否為ie if (navigator.userAgent.indexOf("MSIE") > 0) {
//判斷是否為ie6 if (navigator.userAgent.indexOf("MSIE 6.0") > 0) { window.opener = null; window.close(); } else { window.open(‘‘, ‘_top‘); window.top.close(); } }
//判斷是否為firefox else if (navigator.userAgent.indexOf("Firefox") > 0) { window.location.href = ‘about:blank ‘; }
//其他非firefox等主流瀏覽器如chrome,safari else { window.opener = null; window.open(‘‘, ‘_self‘, ‘‘); window.close(); } } </script>

上面的demo基本可以解決關閉瀏覽器的當前視窗的功能,至於Firefox的實現還是比較遺憾!

相容所有瀏覽器的視窗關閉方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.