Why does calling History.pushState() (or .replaceState()) trigger a 'popstate' event?

801 views Asked by At

I am changing the url depending upon an AJAX call.

window.history.pushState("", "", ''+newUrlWithParameters);

Also I have a popstate event written within my JS.

$(window).on('popstate', function(e) {
   //... do this
});

As soon as The pushState is being called, the popstate is triggered. How to prevent this?

1

There are 1 answers

0
Liam On

I had the same issue - solved it by adding

event.preventDefault();
event.stopPropagation();

to the calling event's handler (they handle a click event on an HTML a element, and tracing through jquery the mouseevent seemed to want to trigger the popState).