js判断页面是关闭还是刷新

JavaScript010

js判断页面是关闭还是刷新,第1张

<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)">

<script type="text/javas

cript">

function close(evt) //author: sunlei

{

var isIE=document.all?true:false

evt = evt ? evt :(window.event ? window.event : null)

if(isIE){//IE浏览器

var n = evt.screenX - window.screenLeft

var b = n >document.documentElement.scrollWidth-20

if(b &&evt.clientY<0 || evt.altKey){

alert("是关闭而非刷新")

}

else{

alert("是刷新而非关闭")

}

}

else{//火狐浏览器

if(document.documentElement.scrollWidth!=0)

alert("是刷新而非关闭")

else

alert("是关闭而非刷新")

}

}

</script>

<body onunload="close(event)">

刷新或关闭页面都会触发onbeforeunload和onunload事件,浏览器没有提供这样的接口来判断是刷新或关闭。

不过有一个不怎么靠谱的办法,就是在onbeforeunload和onunload

回调函数

里面判断鼠标的位置是不是负数,如果是的话,也许就是点了关闭按钮,否则的话也许是按了F5。

我想不到其他办法了