如何使用Javascript在onbeforeunload / onunload期间注销 [英] How to logout during onbeforeunload/onunload using Javascript

查看:60
本文介绍了如何使用Javascript在onbeforeunload / onunload期间注销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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事件应该触发,然后执行我的注销类。

So this is my thought: When the user clicks back, refresh, or closes the window, the standard onbeforeunload pop-up box will appear along with the text located in my message variable. Then, if the user hits Cancel, the onunload event won't be executed and the user will stay on the current page. But if the user hits Okay, then the onunload event should fire off, which then executes my logoff classes.

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

This is what's happening: The pop-up appears when needed and the Cancel button also works. But if the user clicks Okay, it seems like the onunload event fires off too quickly for my logoff classes to execute, because it never logs them off.

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

I know that my logout classes work because if I just call my flex.unloadMethod in the onbeforeunload, it logs them off.

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

I've researched this for a week now and it seems that I'm close but I'm putting something in the wrong place. If you have any examples or advice, it would be appreciated.

推荐答案

所以,我已经发现了一些与我的问题有关的问题在Sunil D的帮助下,onunload事件触发得太快,导致我的事件被触发,因此我决定不包含警告消息,而只是使用onbeforeunload事件将用户全部注销:

So, I've figured out some issues with my question with the help of Sunil D. The onunload event fires too quickly for my event to be triggered, so I've decided not to include a warning message, but just log the user off all together using the onbeforeunload event:

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

这篇关于如何使用Javascript在onbeforeunload / onunload期间注销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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