0

I want to save an unload event to execute it later on. For example:

// on unload do nothing but safe the event
jQuery(window).unload(function() {
            // TODO safe the unload event here somehow
            // unloadEvent = this;
            return false;
});

Later, another piece of code should be able to perform the previously saved event, for example like this:

function finallyUnload() {
        // TODO
        // unloadEvent.perform();
}

Is there any way to do this?

Update

My browser window is communicating with a Java Selenium application and needs to wait until it received the information about the unload event and how it happened. When that's the case, the unload event can be executed.

I understood that unload events cannot be delayed or stopped, so i got another idea. What about intercepting all clicks on links with href and buttons, also submissions of forms. Then i manually set window.location to the desired URL or submit the form. Could that work?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Alp
  • 29,274
  • 27
  • 120
  • 198
  • `I want to save an unload event to execute it later on`. Could you please define `later on`? There's not much javascript executing on your page after the `unload` event. Well, actually there could be, but not on the same page as the user is leaving it. – Darin Dimitrov Nov 23 '11 at 22:13
  • You can't cancel an `unload` event, period. – SLaks Nov 23 '11 at 22:16
  • @Darin: my browser window is communicating with a Java Selenium application and needs to wait till it received the information about the unload event and how it happened. When that's the case, the unload event can be executed. – Alp Nov 23 '11 at 22:20
  • @SLaks: i dont want to cancel it, i want to postpone it – Alp Nov 23 '11 at 22:20
  • You can't do that either. `return false` won't do anything. – SLaks Nov 23 '11 at 22:21
  • what about e.preventDefault()? http://api.jquery.com/event.preventDefault/ – Alp Nov 24 '11 at 00:34

1 Answers1

0

I found a workaround. I intercept all click and submit events and save them for later use after my application pulled the needed event data.

But it does not work in all cases: Intercept JavaScript unload events

Community
  • 1
  • 1
Alp
  • 29,274
  • 27
  • 120
  • 198