Dee
Dee

Reputation: 3255

jQuery or JS handling browsers back button

Any ideas how to intercept the browser back button via jQuery, so I can run my event function? I don't need to use jQuery BBQ or jQuery Address, only prevent the default behaviour and run it later after some animate.

Thank you!

d

Upvotes: 3

Views: 10529

Answers (2)

Chestone
Chestone

Reputation: 564

In jQuery land:

$(window).unload( function () { /*code here*/ });

EDIT- There was a parentheses instead of a close bracket

EDIT - Removed Extra Semicolin

Upvotes: 0

Hernantz
Hernantz

Reputation: 566

Have not tried but i guess this do what you want.

window.onbeforeunload = function () {
   // stuff do do before the window is unloaded here.
}

with jQuery you could try the .unload() function

The unload event is sent to the window element when the user navigates away from the page. This could mean one of many things. The user could have clicked on a link to leave the page, or typed in a new URL in the address bar. The forward and back buttons will trigger the event. Closing the browser window will cause the event to be triggered. Even a page reload will first create an unload event.

Upvotes: 1

Related Questions