2

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.

donk
  • 1,540
  • 4
  • 23
  • 46
  • 2
    it is bad idea to change the normal functionality of the UI elements (like back button). If you are using AJAX to load the pages then it is okay to use History plugin of jQuery. – TheVillageIdiot Jan 28 '11 at 11:03

2 Answers2

2

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.

Caspar Kleijne
  • 21,552
  • 13
  • 72
  • 102
1

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

Serkan Hekimoglu
  • 4,234
  • 5
  • 40
  • 64
Poelinca Dorin
  • 9,577
  • 2
  • 39
  • 43
  • it's not just a page, but a full-blown system, and the content that shows up is being loaded in one div or another with load() function. So the thing is, I have a button put in the system, which reloads the information that was shown before. It's not very intuitive though, and I would like to make the browsers' Back button to call the same event as that button input in the system does. Is it possible? – donk Jan 28 '11 at 11:12
  • I don't now for shure , but i don't think any browser will allow you to change the bahaviour of the back button ( or any other button for that matter ) . – Poelinca Dorin Jan 28 '11 at 11:19