Thursday, February 21, 2013

'Page Unload' triggers too late

Well... too late is a bit odd to say. Because it does NOT triggers too late, it triggers when the page is unloaded!

You can define a dynamic action which reacts on the 'Page Unload' trigger like this:

Most of the times very useful but say, in the hypothetical situation, you want to save your form data when the user presses log off (or the nice button with the little cross in the top corner of their browser window) it doesn't work quite well.

And that has all to do with sessions. Once you logged off you immediately are being directed to the new log on window with a new session ID and the event cannot be handled correctly anymore.
Not to speak of when you close the browser window.

So what's next?

In  APEX 4.2 it is possible to react on a custom event. So should it be possible to react on an event triggered by the 'window' object? Yes it should!

Let's change the event type in the example to 'Custom':

In my opinion the 'Custom Event' field is placed to high and it should be placed underneath the 'DOM Object' field. So let's start with the 'Selection Type'...
You can choose all the known types e.g. item, button, region etcetera. In my example I will use the 'DOM Object'. As an object I will choose 'window' and as a 'Custom Event' the 'beforeunload' event is used.

The beforeunload trigger possibly will fire to often in this example (reload, leave, submit, close) so you can use a JavaScript condition if you want it to fire only in the right situation.

It works like a charm for me :), hope it helped you too.

1 comment:

  1. Thanks for this - nice tip on how to use custom events.
    Shame browser support isn't limited for page unload; and I believe beforeunload is proprietory - specific to IE?