3

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

Gert Grenander
  • 16,866
  • 6
  • 40
  • 43
Dee
  • 3,185
  • 10
  • 35
  • 39

2 Answers2

1

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.

Hernantz
  • 566
  • 1
  • 6
  • 19
0

In jQuery land:

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

EDIT- There was a parentheses instead of a close bracket

EDIT - Removed Extra Semicolin

Red2678
  • 3,177
  • 2
  • 29
  • 43
Chestone
  • 554
  • 4
  • 13
  • 1
    According to [docs](http://api.jquery.com/unload/) it doesn't specifically intercepts back button but any event that unloads current page. It can be anything clicking on link, forward button or back button based on the browser. – Mahesha999 Feb 13 '13 at 14:36
  • This is true Mahesha, unload is fired on navigation away from the current page. As far as I know there is no specific event for the 'back' button. – Chestone Feb 13 '13 at 22:09