patrick
patrick

Reputation: 6860

Using onpopstate in an Chrome Extension for Facebook

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?

Upvotes: 2

Views: 766

Answers (1)

Max Wallace
Max Wallace

Reputation: 3758

@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.

Upvotes: 0

Related Questions