標籤: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小石頭 部落格園