js如何区分页面是关闭还是刷新

JavaScript014

js如何区分页面是关闭还是刷新,第1张

简单版本(没办法判断多选项卡的浏览器):

<mce:script type="text/javascript"><!--

    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("是关闭而非刷新")

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

复杂版本(除非用任务栏关闭):

function CloseOpen(event) { 

     if(event.clientX<=0 || event.clientY<0) { 

       //获取当前时间

      var date=new Date()

      //将date设置为过去的时间

      alert("关闭网页")

      date.setTime(date.getTime()-10000)

      //将userId这个cookie删除

      document.cookie="zhuangtaoexpire="+date.toUTCString()

      document.cookie="quanxianzifucuanexpire="+date.toUTCString()

      document.cookie="quanxianexpire="+date.toUTCString()

     s0 += "关闭窗口!" sw = 1

      onbeforeunload()

      // window.event.returnValue = '关闭浏览器将退出系统.'

     } 

     else

     { 

        alert("刷新或离开") 

     } 

 } 

        

        var currentKeyCode = -1

 

  function document.onkeydown() { // 本窗口的所有下属页面都必须含有本函数

 

    top.currentKeyCode = event.keyCode

  }

 

  function onbeforeunload(){

   

   var sw = 0, s0 = ""

    if (currentKeyCode == 116) 

    {

      s0 += "刷新窗口!(F5)"

    }

    else

    {

      if ((event.altKey)&&(currentKeyCode == 115)) 

      {

         s0 += "关闭窗口!(alt+F4)" sw = 1

          //获取当前时间

          var date=new Date()

          //将date设置为过去的时间

         alert("关闭窗口")

          date.setTime(date.getTime()-10000)

          //将userId这个cookie删除

          document.cookie="zhuangtaoexpire="+date.toUTCString()

          document.cookie="quanxianzifucuanexpire="+date.toUTCString()

          document.cookie="quanxianexpire="+date.toUTCString()

      }

      else

      {

        if ((event.clientX > 0)&&(event.clientX < document.body.clientWidth)) 

        {

         s0 += "刷新窗口!"

        }

        else

        {

        //获取当前时间

          var date=new Date()

          //将date设置为过去的时间

          alert("关闭网页")

          date.setTime(date.getTime()-10000)

          //将userId这个cookie删除

          document.cookie="zhuangtaoexpire="+date.toUTCString()

          document.cookie="quanxianzifucuanexpire="+date.toUTCString()

          document.cookie="quanxianexpire="+date.toUTCString()

         s0 += "关闭窗口!" sw = 1

        }

      }

    }

    if (sw == 1) 

    {

     event.returnValue = ""

    }

    else

    {

     currentKeyCode = -1

    }

   

  }

 

<body onunload="CloseOpen(event)" ></body></html>

<body>标签只有onload\onunload\onbeforeunload事件,而没有onclose事件。不管页面是关闭还是刷新都会执行onunload事件。如何捕捉到页面关闭呢?

页面加载时只执行onload

页面关闭时只执行onunload

页面刷新时先执行onbeforeunload,然后onunload,最后onload。这样我们可以在onbeforeunload中加一个标记,在onunload中判断该标记,即可达到判断页面是否真的关闭了。

<html>

<head>

<title>判断页面是关闭还是刷新</title>

</head>

<body onunload="fclose()" onload="fload()" onbeforeunload="bfunload()">

<script language="javascript">

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>