window.onpopstate
是用来监听浏览器的回退前进事件。
与window.onpopstate相关的H5新方法:
history.pushState(state,null,url)
历史记录中添加一个状态1234history.pushState({page : 'index',id : 1},null,'index.html');history.replaceState(state,null,url)
替换一条历史记录123history.replaceState({page : 'index'},null,'index.html')
在onpopstate事件中,会调取到pushState和replaceState方法中state对象,比如:123window.onpopstate = function(e){ alert(e.state.page);}
栗子:给当前页面添加一个历史记录(可跨域)
now.html当前访问的页面添加onpopstate事件12345window.onpopstate = function(e){ location.reload();// 回退页面刷新}history.replaceState({page : 'other'},null,'other.html');//替换历史状态history.pushState({page : 'now'},null,'now.html');//创建历史状态
other.html跳转页面重定向,实现跨域1location.href = 'index.html';//可以跨域