1

I've implemented the Flex / js logic to prompt the user when they try to navigate away from my app (e.g. close browser, hit back button, etc) described here. This works great and I've been super happy with the results. One quick question that recently came up is how to capture the "user pressed ok on the js popup and is in fact leaving the page" event from inside my flex app, in order to know when the user actually left the page. I assume the logic is similar, but unfortunately I do not speak js and am thus stuck. Any help would be greatly appreciated.

Thank you!

Community
  • 1
  • 1
fred august
  • 1,109
  • 12
  • 31
  • This is quite unclear, as you mentioned that the prompt would display when the browser is closed, and when you click OK, the browser would then have been closed, thus, your SWF would be unable to capture the OK response. – Batuta Mar 07 '11 at 22:19
  • sorry for the lack of clarity - the prompt displays before the browser is closed by listening to the js event onbeforeunload (that's the whole point of having a prompt). – fred august Mar 07 '11 at 22:40

3 Answers3

0

onbeforeunload is not designed to give you additional time with the page if the user chooses to say "yes, let me leave" - there's nothing you could do with the information that the user did close the page. You can tell Flash that the function ran, and possibly do something with that, depending on how fast the user presses 'ok' but I believe that's about it.

Aaron
  • 4,634
  • 1
  • 27
  • 43
  • interesting... so no way to hook up a callback to the button presses? – fred august Mar 08 '11 at 16:32
  • Not with onbeforeunload. If functionality like that existed it would delay the user leaving the page, and for how long? Either that or would be used to abusively keep users on a certain page. – Aaron Mar 17 '11 at 20:16
  • I would never (ever) try to stop a user from leaving my page. What I've implemented is the pretty standard "are you sure you want to leave" alert when data has not been saved (see for instance gmail and many other web apps). What I would like to do is figure out how long my session was. I know when it started but I don't know when it ends. I would if the user didn't have an option to cancel, but now I don't anymore. Thus my question. I'm open to whatever other solution - doesn't have to be listening to the ok button on the alert. – fred august Mar 18 '11 at 01:17
  • Not saying you would, I was using that to explain why it is implemented the way it is. You can absolutely report back to your application when you're about to display the ok/cancel prompt, but you can't report back to your application based on whether the person says ok. – Aaron Mar 18 '11 at 14:58
0

The result of the prompt function should be value of the text field.
Although, you can not keep the browser from closing/unloading(security) you will have a little bit of time before the unload starts. So you could do limited data management here however, it is not recommended and data can/will be lost.
If you are doing it just to remind the user that you think they should stay at your site, you might want to reconsider this as it is most annoying and will cause you to loose more users.

<script type="text/javascript">
  var answer = prompt ("What is your name ?","")
  alert ("Hello there, " + answer)
</script> 
The_asMan
  • 6,364
  • 4
  • 23
  • 34
  • I guess I should have explained my ultimate goal: – fred august Mar 18 '11 at 00:57
  • I would never (ever) try to stop a user from leaving my page. What I've implemented is the pretty standard "are you sure you want to leave" alert when data has not been saved (see for instance gmail and many other web apps). What I would like to do is figure out how long my session was. I know when it started but I don't know when it ends. I would if the user didn't have an option to cancel, but now I don't anymore. Thus my question. I'm open to whatever other solution - doesn't have to be listening to the ok button on the alert. – fred august Mar 18 '11 at 01:15
0

Ok, I think I found my answer - I think hooking up onunload will do the trick, as it'll let me discern between actual departures from the page and attempted ones. Thank you to Erik Bakker for his answer in this thread (upvoted!).

thank you all for your help

f

Community
  • 1
  • 1
fred august
  • 1,109
  • 12
  • 31