今天测试一个h5中的popstate事件,虽然我很久之前就对这个新属性有所学习,
https://blog.csdn.net/zgrbsbf/article/details/25297359
无奈今天还是犯了几个小错误,记录如下:
错误:
https://developer.mozilla.org/en-US/docs/Web/Events/popstate
这篇文章中说,
The popstate event is fired when the active history entry changes.
然后我写了个例子测试,2个页面,每个中都有a链接,两个页面互相跳转,两个页面都对window添加了popstate事件,无奈就是不触发。难道是我写的有什么问题吗,我感觉没有啊。
正确:
后来我又写了个页面,新页面中pushState了一下,也添加了popstate事件,搞了个定时器history.back之后,pop触发了!
https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onpopstate
A popstate event is dispatched to the window each time the active history entry changes between two history entries for the same document.
同一个文档中的两个历史记录,这是重点。但是第一个文献中就没讲清楚,害得我误解。