0

I'm facing a strange issue where where popstate returns null, regardless, even if pushState has fired seven or eight times.

window.addEventListener('popstate', function(event) {

    console.log(event.state);

});

I have also tried:

window.onpopstate = function(event) {
        console.log(event.state);
};

But no luck. I'm using Chrome.

try-catch-finally
  • 7,436
  • 6
  • 46
  • 67
Adam
  • 9,189
  • 15
  • 46
  • 62
  • According to the documentation ... "Note that just calling history.pushState() or history.replaceState() won't trigger a popstate event. The popstate event is only triggered by doing a browser action such as a click on the back button (or calling history.back() in JavaScript)." ... https://developer.mozilla.org/en-US/docs/Web/Events/popstate – rfornal Dec 25 '14 at 02:44
  • Right, I should have clarified, I still get null even while using the browsers back or forward buttons. – Adam Dec 25 '14 at 02:45
  • possible duplicate of [window bind POPSTATE](http://stackoverflow.com/questions/4688164/window-bind-popstate) – OnlyMAJ Dec 25 '14 at 02:46
  • OK ... I also noticed that the `state` was sometimes empty; do you get anything with `console.log(event)`? – rfornal Dec 25 '14 at 02:46
  • @OnlyMAJ This is not a duplicate. I do not care whether this fires on initial load, I only want popstate to work when navigating using the browsers forward/back buttons. – Adam Dec 25 '14 at 03:01
  • @rfornal I do get something with event. It shows PopStateEvent {state: null, clipboardData: undefined, path: NodeList[0], cancelBubble: false, returnValue: true…} – Adam Dec 25 '14 at 03:04
  • Not sure why this was associated with a question of a different issue? – Adam Dec 25 '14 at 20:12

1 Answers1

0

The problem was I was not correctly setting the pushState variable.

Adam
  • 9,189
  • 15
  • 46
  • 62