Andrew Bullock
Andrew Bullock

Reputation: 37378

DOM onUnload doesn't fire until response begins

Consider the following code:

$(window).unload(function () {
    console.log('foo');
});

If I trigger the unload event by navigating to a different URL, the event doesn't fire until the browser begins to get a response.

i.e. if the DNS lookup takes a long time, or the remote server takes time to respond (the loader animating anti-clockwise in Firefox, the event only fires once it starts rotating clockwise)

Is there another event I can listen to that happens as soon as navigation begins?

I realise I could attach to link clicks and intercept there, but that's just going to be hacky because there countless edge cases I'd have to manually capture here. OnUnload is the sensible place, but it doesn't behave as I want :(

thoughts?

Upvotes: 0

Views: 72

Answers (1)

Garry Shutler
Garry Shutler

Reputation: 32698

There's window.onbeforeunload that might do what you're looking for.

Upvotes: 2

Related Questions