Using onpopstate in an Chrome Extension for Facebook

728 Views Asked by At

I'm trying to create a Chrome plugin for facebook and I'm using onpopstate event to check when the user goes to another page. The only problem is that the onpopstate doesn't fire.

This is the (simple) code I'm using:

window.onpopstate = function() { console.log('pop'); };

this is a screen of the problem:

code test in console

As you can see the pushState code is called, but the onpopstate listener is not.

Do you know what's happening here?

1

There are 1 best solutions below

0
Max Wallace On

@enhzflep's answer is in the comments above and not immediately obvious. From MDN:

Calling history.pushState() or history.replaceState() won't trigger a popstate event. The popstate event is only triggered by performing a browser action, such as clicking on the back button (or calling history.back() in JavaScript), when navigating between two history entries for the same document.

In other words, the onpopstate event shouldn't be firing in this case.