js监听页面关闭或刷新

需求:在页面关闭的时候监听事件,但是刷新的时候并不执行。

实现:

window.onbeforeunload = function(){

      var  n  =  window.event.screenX  -  window.screenLeft; 

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

      if(b  &&  window.event.clientY  <  0  ||  window.event.altKey) 

      {  //页面关闭

      }else{

          // 页面刷新   

    } 

}


猜你喜欢

转载自blog.csdn.net/qq_33168578/article/details/79658909