donk
donk

Reputation: 1570

trigger event on browser back button click

How do I listen for a browser back button click and call a function that I've defined and disable the normal behaviour of the back button? I'm using jQuery.

Upvotes: 2

Views: 25184

Answers (2)

Poelinca Dorin
Poelinca Dorin

Reputation: 9703

You could play with :

$(window).unload( function () { alert("Bye now!"); } );

.unload()

But this will be trigered when the user clicks a link that goes to another page , types a new address in the address bar or ... basicly it will be trigered whenever the user leaves the current page . I don't think you can stop the user from going away from you're page tough .

UPDATE:

unload() is now deprecated from jquery 1.8

Upvotes: 1

Caspar Kleijne
Caspar Kleijne

Reputation: 21864

jQuery history plugin helps you to support back/forward buttons and bookmarks in your javascript applications. You can store the application state into URL hash and restore the state from it.

Upvotes: 2

Related Questions