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>

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.

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.

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.

Brian
  • 529
  • 2
  • 7
  • 17
  • The problem is likely happening because the browser doesn't really give you enough time for your event handlers to do their work. Because the user is navigating away, you want to execute as little code as possible in these scenarios to give your code enough time to complete. I would try triggering your logout.jsp with an AJAX call from Javascript, not from Flash. – Sunil D. May 21 '13 at 21:28
  • Yes, thanks for the comment. I realize now that what I wanted to do seems to just not be possible. Instead, I'm going to bypass the pop-up completely so that my logout.jsp will execute. (see answer) Thanks again for your response! – Brian May 22 '13 at 14:40

1 Answers1

2

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>
Brian
  • 529
  • 2
  • 7
  • 17