首页 > 技术文章 > 用js实现关闭页面后台事件

axin85 2017-07-27 11:41 原文

前台代码:

window._onbeforeunload = function()
{
    $('txtrefchild').click();
    return true;
}

后代代码:

protected void txtrefchild_Click(object sender, EventArgs e)
{
    Session.Remove("dataset");
}

<script language="javascript">  

window.onbeforeunload = function()     

{     

if(!(event.clientX>document.body.clientWidth&&event.clientY<0||event.altKey))     

    {     

//在刷新、或以_self方式转到其它页面、或用ALT+CTLR+DEL关闭时触发;  

 //代码

   }

else  

      {  

//点击工具栏上的[X]按钮、或ALT+F4关闭时触发   

      }  

}  

</script>  

window.onbeforeunload  =  function()     

      {     

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

           {            

//alert("关闭触发");  

            }  

else   {  

//alert("刷新触发");  

           }  

  }  

推荐阅读