浏览器的卸载事件 [英] on unload event of browser

查看:71
本文介绍了浏览器的卸载事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI

我想从客户端页面超时会话,我尝试使用下面的代码,但无法执行代码,需要一些如何处理用户的OK或CANCEL事件的帮助

I wanna timeout session from client page,i tried below code but not able to execute code need some help of how i can handle OK or CANCEL events from user

<script>

 function onBeforeUnloadAction(){

    return "Would u like to discard Changes";

 }

 window.onbeforeunload = function(){
   if((window.event.clientX<0) ||
      (window.event.clientY<0))
{
     return onBeforeUnloadAction();

   }
 }

 </script>

推荐答案

onbeforeunload 必须返回一个字符串,浏览器会根据该字符串创建确认框.如果您想捕获用户没有离开(如果用户离开,则无法运行任何东西,就是这样),只需设置一个计时器,看看它是否触发:

onbeforeunload must return a string, the browser creates the confirm box from that string. If you want to capture that the user didn't leave (You cannot run anything if the user left, thats just how it is), simply set a timer and see if it fires:

window.onbeforeunload = function(){
    setTimeout(function(){
        //User didnt leave
        goDoFunStuff();
    },500);
    return 'wanna go?'
}

由于 onbeforeunload 被阻止,计时器只会在用户没有离开时触发

Since onbeforeunload is blocking the timer will only fire if the user didnt leave

这篇关于浏览器的卸载事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆