popstate returns event.state is undefined

后端 未结 1 495
野的像风
野的像风 2020-12-11 00:09

I am learning about history in HTML5, in this example (open the JavaScript browser console to see error) the event.state.url returns:

Uncaught T         


        
相关标签:
1条回答
  • 2020-12-11 00:38

    event is the jQuery event object, not the DOM one.

    To access the DOM event object, use event.originalEvent: http://jsfiddle.net/pimvdb/un4Xk/1/.

    var state = event.originalEvent.state;
    

    Remember that the state is only defined when the new state has data, so it is not available when clicking and then going back to the initial state:

    1. initial state
    2. link to state 1
    3. back button to initial state (no data available)

    It is, however, available when clicking, clicking another time and then going back:

    1. initial state
    2. link to state 1
    3. link to state 2
    4. back button to state 1 (data available)
    0 讨论(0)
提交回复
热议问题