Difference between a pressed back button and a forward button pressed using the history API

I am using api history to enter a new URL into a web page without reloading. I have several buttons that have different functionality.

My script now works with almost no problems. When I click the button, something happens, and when I return, the script starts the event listener without reloading the page.

However, when I press the forward button now, I want to go forward. The URL has been changed correctly to the next, but the event listener still fires as if the back button had been pressed

Example

index1.html: button press -> index2.html -> button press -> index 3.html -> back button pressed -> index2.html -> forward button pressed -> URL is now index3.html, content however is index1.html

I guess this is because I have a listener that listens to the popstate that occurs when the back and forward buttons are clicked. How can I tell which button was pressed?

This is the part that binds the listener.

 if (window.history && window.history.pushState) { $(window).unbind('popstate'); $(window).bind('popstate', function (e) { clearOverlays(); var url = URL $.ajax ( { url : url }).done ( function ( data ) { console.log(data); }); }); } 
+7
javascript html html5 browser-history html5-history
source share

No one has answered this question yet.

See similar questions:

182
How to detect browser back button event - Cross Browser
52
How can I get if a popstate event comes from a reverse or direct action using pushstate HTML5?

or similar:

twenty
When the back button launches popState, how can I prevent the page from refreshing?
14
Disable HTML5 History Button
10
HTML5 History API: JSON is displayed when going to another page, and then again "forward"
7
Clear popup event history
2
How to change chrome to behave like firefox when using history.pushState / popstate
one
History. Popstate does not work and is called for both the front and back buttons in the browser
one
HTML5 story combined with submit form
0
How to manage browser history and catch browser buttons back / forward?
0
HTML5 History API: "popstate" is not called when navigating from another page
0
window.history.pushState back button works, but does not redirect after clicking

All Articles