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,它会将它们注销。

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

4

1 回答 1

2

因此,在 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>
于 2013-05-22T14:42:45.213 回答