What are the differences between history.pushState & location.hash? [closed]

location.hash has a better support than the history.pushState method. The advantage of the pushState method is that you can bind a state to the history entry. If you don’t need this state object, I recommend to use the location.hash property, to have better compatibility with older browsers. location.hash=”new-hash”; console.log(history.state); // null or undefined history.pushState({extraData: “some … Read more

How do I retrieve if the popstate event comes from back or forward actions with the HTML5 pushstate?

You must implement it yourself which is quite easy. When invoking pushState give the data object a unique incrementing id (uid). When onpopstate handler is invoked; check the state uid against a persistent variable containing the last state uid. Update the persistent variable with the current state uid. Do different actions depending on if state … Read more

How to trigger change when using the back button with history.pushstate and popstate?

The popstate only contains a state when there is one. When it goes like this: initial page loaded new page loaded, with state added via pushState back button pressed then there is no state, because the initial page was loaded regularly, not with pushState. As a result, the onpopstate event is fired with a state … Read more

How to detect when history.pushState and history.replaceState are used? [duplicate]

I used to use this to also be notified of when pushState and replaceState are called: // Add this: var _wr = function(type) { var orig = history[type]; return function() { var rv = orig.apply(this, arguments); var e = new Event(type); e.arguments = arguments; window.dispatchEvent(e); return rv; }; }; history.pushState = _wr(‘pushState’), history.replaceState = _wr(‘replaceState’); … Read more