无论是从页签处关闭浏览器,还是关闭整个浏览器窗口,无论是 ie11,火狐,谷歌,苹果,还是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小石头 博客园