首页 文章

如何使用Javascript在onbeforeunload / onunload期间注销

提问于
浏览
1
<script type="text/javascript">

window.onbeforeunload=before;
window.onunload=after;

function before(evt)
{
   var message="If you continue, your session will be logged out!";
   if(typeof evt=="undefined"){
      evt=window.event;
   }
   if(evt){
      evt.returnValue=message;
   }
}

function after()
{
   var flex=document.${application}||window.${application};
   flex.unloadMethod(); //calls the flex class containing the "unloadMethod()" which
                        //calls the logout.jsp that does the actually dropping of credentials
}

</script>

所以这是我的想法:当用户点击,刷新或关闭窗口时,标准的onbeforeunload弹出框将与我的消息变量中的文本一起出现 . 然后,如果用户点击取消,则不会执行onunload事件,并且用户将保持在当前页面上 . 但是如果用户点击Okay,那么onunload事件应该触发,然后执行我的注销类 .

这就是发生的事情:弹出窗口在需要时出现,取消按钮也可以 . 但是如果用户单击Okay,则onunload事件似乎会因为我的注销类执行而过快地触发,因为它从不会将其记录下来 .

我知道我的注销类是有效的,因为如果我只是在onbeforeunload中调用我的flex.unloadMethod,它会将它们记录下来 .

我已经研究了一个星期了,似乎我很接近但是我把东西放错了地方 . 如果您有任何示例或建议,将不胜感激 .

1 回答

  • 1

    所以,我在Sunil D的帮助下找出了我的问题的一些问题.onunload事件触发太快,我的事件被触发,所以我决定不包含警告信息,但只是记录用户关闭一起使用onbeforeunload事件:

    <script type="text/javascript">
    
    window.onbeforeunload=before;
    window.onunload=after;
    
    function before(evt)
    {
       var flex=document.${application}||window.${application};
       flex.unloadMethod(); //calls the flex class containing the "unloadMethod()" which
                            //calls the logout.jsp that does the actually dropping of credentials
    }
    
    function after(evt)
    {
    
    }
    
    </script>
    

相关问题