26

I'm coding a little demo for the History API. And I'm struggling with this:

$(window).bind('popstate',  
    function(event) {
        console.log('pop: ' + event.state);
    });

It logs 'pop: undefined' when I click on the 'Previous' button...

But if I do this instead, things are working like expected :

window.onpopstate = function(event) {
    console.log('pop: ' + event.state);
};

It logs 'pop: [object Object]' this time...

So it's like jQuery doesn't pass the event object to the callback.
Is there a problem with jQuery ? Or did I mess something ?

Tim Tisdall
  • 8,670
  • 3
  • 41
  • 70
Jocelyn LECOMTE
  • 830
  • 1
  • 7
  • 15
  • Which browser? Have you seen http://stackoverflow.com/questions/4688164/window-bind-popstate yet? – Matt Ball Apr 24 '12 at 14:28
  • thx for your comment, it helped to me to understand better what's the problem, so I changed a little bit my question. It is not the same than the one that you point, because it's not a problem on the first load. – Jocelyn LECOMTE Apr 24 '12 at 14:53

1 Answers1

38

In the first instance, you're getting a normalized jQuery event object. In the second instance, you're getting the browser's event object. I assume that jQuery hasn't completed normalizing all the new HTML5 events and related attributes. Until then, you'll need to access the original event object. You can do that through the jQuery event object via the originalEvent property. You can find some additional details and examples here: stackoverflow.com/questions/7860960/popstate-returns-event-state-is-undefined

event.originalEvent.state
Community
  • 1
  • 1
KSev
  • 1,799
  • 1
  • 20
  • 22
  • Nice catch! I didn't see this question when I searched before asking :( – Jocelyn LECOMTE Apr 24 '12 at 15:35
  • Thanks! I'd like to take full credit for the catch but stackoverflow did it for me. I happened to notice it in the Related list in the right margin. All the best in your development efforts! – KSev Apr 24 '12 at 15:46