js 如何判断页面是否刷新???

JavaScript034

js 如何判断页面是否刷新???,第1张

//check for Navigation Timing API support

if (window.performance) {

console.info("window.performance works fine on this browser")

}

if (performance.navigation.type == 1) {

console.info( "This page is reloaded" )

location.hash='/app/homepage'

} else {

console.info( "This page is not reloaded")

}

扩展资料

判断一个网页是刷新还是关闭

<html>

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=gb2312">

<title>新建网页 1</title>

</head>

<body οnunlοad=fclose() οnlοad=fload() οnbefοreunlοad=bfunload()>

<script>

var s = "test"

function fclose()

{

if(s=="no")

alert('unload me!='+s+'这是刷新页面!')

else

alert('这是关闭页面')

}

function fload()

{

alert("load me!="+s)

}

function bfunload()

{

s = "no"

}

</script>

</body>

</html>

<mce:script type="text/JavaScript"><!--function close(evt) //author: sunlei{var isIE=document.all?true:falseevt = evt ? evt :(window.event ? window.event : null)if(isIE){//IE浏览器var n = evt.screenX - window.screenLeftvar b = n >document.documentElement.scrollWidth-20if(b &&evt.clientY<0 || evt.altKey){//alert("是关闭而非刷新")window.location.href="../include/logout.PHP"}else{//alert("是刷新而非关闭")return false}}else{//火狐浏览器if(document.documentElement.scrollWidth!=0){//alert("是刷新而非关闭")//window.location.href="report_list.php?ss=1"return false}else{alert("是关闭而非刷新")//window.location.href="repost_list.php?ss=0"//alert("bbbbbbb")}}}// --></mce:script><BODY onunload="close(event)">

页面加载时只执行onload 页面关闭时只执行onunload 页面刷新时先执行onbeforeunload,然后onunload,最后onload。经过验证我得出的结论是://对于ie,谷歌,360://页面加载时只执行onload//页面刷新时,刷新之前执行onbeforeunload事件,在新页面即将替换旧页面时onunload事件,最后onload事件。//页面关闭时,先onbeforeunload事件,再onunload事件。//对于火狐://页面刷新时,只执行onunload;页面关闭时,只执行onbeforeunload事件那么回归正题,到底怎样判断浏览器是关闭还是刷新?我按照网上的各种说法实验千百遍,都未成功,其中各种说法如下:window.onbeforeunload = function() //author: meizz{var n = window.event.screenX - window.screenLeftvar b = n >document.documentElement.scrollWidth-20if(b &&window.event.clientY <0 || window.event.altKey){alert("是关闭而非刷新")window.event.returnValue = ""//这里可以放置你想做的操作代码}else{alert("是刷新而非关闭")}}window.onbeforeunload = function() //author: meizz{var n = window.event.screenX - window.screenLeftvar b = n >document.documentElement.scrollWidth-20if(b &&window.event.clientY <0 || window.event.altKey){alert("是关闭而非刷新")window.event.returnValue = ""//这里可以放置你想做的操作代码}else{alert("是刷新而非关闭")}}和function CloseOpen(event) {if(event.clientX<=0 &&event.clientY<0) {alert("关闭")}else{alert("刷新或离开")}}这些方法都不管用,但是我并没有放弃,想啊想啊........按照上面我得出结论,//对于ie,谷歌,360://页面加载时只执行onload//页面刷新时,刷新之前执行onbeforeunload事件,在新页面即将替换旧页面时onunload事件,最后onload事件。//页面关闭时,先onbeforeunload事件,再onunload事件。//对于火狐://页面刷新时,只执行onunload;页面关闭时,只执行onbeforeunload事件刷新的时候先onbeforeunload,然后取服务端请求数据,在新页面即将替换旧页面时onunload事件,而页面关闭时,先onbeforeunload事件,再立即onunload事件。那么在刷新的时候,onbeforeunload与onunload之间的时间肯定比关闭的时候时间长,经过测试确实如此。贴出我的测试代码:var _beforeUnload_time = 0, _gap_time = 0var is_fireFox = navigator.userAgent.indexOf("Firefox")>-1//是否是火狐浏览器window.onunload = function (){_gap_time = new Date().getTime() - _beforeUnload_timeif(_gap_time <= 5)$.post(pathName+"/back/bi!aaaa.s2?t="+_beforeUnload_time,{msg:"浏览器关闭",time:_gap_time},function(json){},"text")else$.post(pathName+"/back/bi!aaaa.s2?t="+_beforeUnload_time,{msg:"浏览器刷新",time:_gap_time},function(json){},"text")}window.onbeforeunload = function (){_beforeUnload_time = new Date().getTime()if(is_fireFox)//火狐关闭执行$.post(pathName+"/back/bi!aaaa.s2?t="+_beforeUnload_time,{msg:"火狐关闭"},function(json){},"text")}