9

Just to ensure, I am NOT asking how to catch back/forward button events.

window.addEventListener('popstate', function (e) {
                       alert("Yahoooo!!!!"); 
                   });

That function above is triggered either it is browser's back button or browser's forward button but I cannot detect which one was clicked? Was it back button or forward button?

Is there any specific event for them that I can detect it was "back" or "forward" button specifically?

EDIT: While I appreciate the "duplicate question" suggestion, the similar question posted here on stackoverflow but the accepted answer is from January 2012!! A lot of things changed since then. Maybe there is already easier way for this!

Thanks

Community
  • 1
  • 1
curiousBoy
  • 5,431
  • 4
  • 43
  • 51
  • 1
    Possible duplicate of [How do I retrieve if the popstate event comes from back or forward actions with the HTML5 pushstate?](http://stackoverflow.com/questions/8980255/how-do-i-retrieve-if-the-popstate-event-comes-from-back-or-forward-actions-with) – pizzarob Feb 03 '17 at 23:22
  • The accepted answer is "walk around" and 4 years old for that question!! There may be some easier way around by the time of now! – curiousBoy Feb 03 '17 at 23:37
  • You are right, its 2012, but it is still the way to detect history navigation. – Ibu Feb 03 '17 at 23:51
  • you'll have to manually keep track of some sort of index – pizzarob Feb 04 '17 at 05:26
  • 1
    I put some [up-to-date code](https://stackoverflow.com/a/49329267/2402790) there, in case it helps. – Michael Allan Mar 16 '18 at 21:25
  • Did you get the answer of this question? – Martin AJ Jun 19 '18 at 19:01
  • @MartinAJ - unfortunately no.. [this](https://stackoverflow.com/questions/8980255/how-do-i-retrieve-if-the-popstate-event-comes-from-back-or-forward-actions-with) is the only closer suggestion but not what I wanted – curiousBoy Jun 20 '18 at 05:23

0 Answers0