终极版---如何区别浏览器刷新和关闭
瀏覽器關(guān)閉窗口時給后臺發(fā)請求 刷新時不做操作
?
// 檢測瀏覽器窗口關(guān)閉
var _beforeUnload_time = 0,_gap_time=0;
window.οnunlοad=function(){
? _gap_time=new Date().getTime()-_beforeUnload_time;
? if(_gap_time<=5){
? //瀏覽器關(guān)閉
? ? ? ? ? $.ajax({
? ? ? ? ? ? ? ? ? url:'/movecheck/logout',
? ? ? ? ? ? ? ? ? // dataType:"JSON",
? ? ? ? ? ? ? ? ? cache:false,
? ? ? ? ? ? ? ? ? type:'post',
? ? ? ? ? ? ? ? ? async:false,
? ? ? ? ? ? ? ? ? success:function(dt){
? ? ? ? ? ? ? ? ? //? console.log(dt)
? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? error:function(dt){
? ? ? ? ? ? ? ? ? }
? ? ? ? ? })
? }
}
window.οnbefοreunlοad=function(){
? _beforeUnload_time=new Date().getTime();
}
});
?
?
========================================
測試源碼 不用做參考
? var?_beforeUnload_time?=?0,?_gap_time?=?0;
????window.onunload?=?function?(){
????????_gap_time?=?new?Date().getTime()?-?_beforeUnload_time;
console.log(?_gap_time)
if(typeof(localStorage["name"])=="undefined"){
localStorage["name"]="240";
}else{
localStorage["name"]=localStorage["name"]-1;
}
????????if(_gap_time?<=?5)?{
? console.log("瀏覽器關(guān)閉")
?$.ajax({
????????type:?"POST",
url:"http://39.104.72.213:80/api/api/accounts/account/AnonymousLogin?ip=192.168.1."+localStorage["name"]",
????????dataType:?"JSON",
????????cache:?false,
????????success:?function(msg){
????????
???????? },
????????error:function(err){
???????? console.log(err)
????????}
?})
}else{
console.log("瀏覽器刷新")
}
????????????
????}
????window.onbeforeunload?=?function?(){
????????_beforeUnload_time?=?new?Date().getTime();
???????
????};
總結(jié)
以上是生活随笔為你收集整理的终极版---如何区别浏览器刷新和关闭的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 模板Struts的UI标签的主题和模板
- 下一篇: SketchUp Pro for Mac