js 判斷瀏覽器關閉事件 相容所有瀏覽器

來源:互聯網
上載者:User

標籤:blog   class   code   java   ext   c   

無論是從頁簽處關閉瀏覽器,還是關閉整個瀏覽器視窗,無論是 ie11,Firefox,Google,蘋果,還是ie6,都能相容的瀏覽器關閉事件監聽

在網上搜尋了一天,雖然網上也有之類的代碼,但是太繁瑣,有時候還不可用。我也是在原有基礎上修改的。經過了上述的瀏覽器測試,如果有不相容的,歡迎提出意見一起學習。

?
<script type="text/jscript" src="jquery-1.10.2.min.js"></script> <script type="text/jscript"> //利用ajax將測試資料儲存 function Ajapost(str){     $.ajax({         type:"POST",         url: "writefile.php?t="+new Date().getTime(),         //將擷取的熱力點儲存到檔案中         data:{content:str+"\n"},         dataType:"text"     }); }     var str=""; function promptOnClose(e){     // 這裡編寫關閉後執行的代碼:注意,關閉之後js不在運行,所以這裡可以用ajax擷取資料將資料交給幕後處理     str+="c";Ajapost(str);//儲存測試資料     e = e ? e : windowevent; } if(window.Event){//主流瀏覽器     str+="a";Ajapost(str);//儲存測試資料     window.onbeforeunload = function(event){         return promptOnClose(event);     } }else{//非主流瀏覽器     str+="b";Ajapost(str);//儲存測試資料     window.onbeforeunload = function(){         return promptOnClose(event);     } }   </script>

 下面是writefile.php

?
<?php file_put_contents("testMess.txt",$_POST[‘content‘],FILE_APPEND); ?>

 testMess.txt,會自動產生,就是所謂的記錄檔

 

轉載請保留來源:27g小石頭 部落格園

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.