zoukankan      html  css  js  c++  java
  • js 判断浏览器关闭事件 兼容所有浏览器

    无论是从页签处关闭浏览器,还是关闭整个浏览器窗口,无论是 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+"
    "},
    		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小石头 博客园

  • 相关阅读:
    类的专有方法(__getitem__和__setitem__)
    类的专有方法(__len__)
    demo02
    python之函数用法__str__()
    repr
    类的专有方法(__repr__)
    类的专有方法(__del__)
    类的专有方法(__init__)
    静态方法
    Golang接口简单了解
  • 原文地址:https://www.cnblogs.com/jsoncode/p/3720699.html
Copyright © 2011-2022 走看看