Search code examples
javascriptjquerybuttonback

How to distinguish browser back / forward button?


Is it possible? even better, is it possible with jQuery?

I need to know whether use click back button or forward button so I can use page transition effect correctly, eg. slide from left - right if they hit forward and vice versa.


Solution

  • You can use the really simple history library

    But if you want to handle it yourself, you can use the hash key (window.location.hash) by setting for instance the slide number there. When the user will click back or next the hash key will change, you detect it and make the change.

    Unfortunately you have to check the change yourself, by setting an interval(i.e: 300ms) and check with the previous value. HTML5 has an onhashchange event.