window.onpopstate = console.log
window.onhashchange = console.log
1. history.pushState({a:1,b:2}, ‘title‘, ‘/test‘) // /test
2. history.pushState({test:2,b:33}, ‘title2‘, ‘/test2‘) // /test2
3. history.back() // /test log: {state:{a:1,b:2},...other}
4. history.forward() // /test2 log: {state:{test:2,b:33}, ...other}
5. history.back()
6. history.pushState({test: 3, b:44}, ‘title3‘, ‘/test3‘) // history.length === 2 /test2被删除
7. location.hash = ‘#test3‘ // 会立刻触发onpopstate与hashchange 并且增加history记录。
8. history.pushState(null,‘‘,‘#a‘) // 不会触发history与hashchange
9. history.back() // 触发history与hashchange
location.href的跳转(不含location.hash="#xxx"),属于刷新页面跳转,不管是history.back() 还是返回按钮,都会刷新页面,不会触发任何事件(hashchange,popstate)。
如果记录了history,则可以使用history.back(),但不一定能使用自带返回按钮,返回上一页,如下:
记录history 点击返回按钮 跳到前一页
原文:https://www.cnblogs.com/gsgs/p/12989831.html