/*Returns the number of entries in the joint session history.*/ window.history.length
/*Returns the current state object.*/ window.history.state
/*Goes back or forward the specified number of steps in the joint session history.A zero delta will reload the current page.If the delta is out of range, does nothing.*/ window.history.go([delta])
/*Goes back one step in the joint session history.If there is no previous page, does nothing.*/ window.history.back()
/*Goes forward one step in the joint session history.If there is no next page, does nothing.*/ window.history.forward()
/*Pushes the given data onto the session history, with the given title, and, if provided and not null, the given URL.*/ window.history.pushState(data, title[url])
/*Updates the current entry in the session history to have the given data, title, and,if provided and not null, URL.*/ window.history.replaceState(data, title[url])
<!DOCTYPE HTML> <!-- this starts off as http://example.com/line?x=5 --> <title>Line Game - 5</title> <p>You are at coordinate <span id="coord">5</span> on the line.</p> <p> <a href="?x=6" onclick="go(1); return false;">Advance to 6</a> or <a href="?x=4" onclick="go(-1); return false;">retreat to 4</a>? </p> <script> var currentPage = 5; // prefilled by server!!!! functiongo(d) { setupPage(currentPage + d); history.pushState(currentPage, document.title, '?x=' + currentPage); } onpopstate = function(event) { setupPage(event.state); } functionsetupPage(page) { currentPage = page; document.title = 'Line Game - ' + currentPage; document.getElementById('coord').textContent = currentPage; document.links[0].href = '?x=' + (currentPage+1); document.links[0].textContent = 'Advance to ' + (currentPage+1); document.links[1].href = '?x=' + (currentPage-1); document.links[1].textContent = 'retreat to ' + (currentPage-1); } </script>
从例子中我们看到一个popstate的事件,这里也看看mozalla官方文档
1 2 3 4 5 6 7 8 9 10 11
An event handler for the popstate event on the window.
A popstate event is dispatched to the window every time the active history entry changes between two history entries for the same document. If the history entry being activated was created by a call to history.pushState() or was affected by a call to history.replaceState(), the popstateevent's state property contains a copy of the history entry's state object.
Note that just calling history.pushState() or history.replaceState() won't trigger apopstate event. The popstate event is only triggered by doing a browser action such as clicking on the back button (or calling history.back() in JavaScript). And the event is only triggered when the user navigates between two history entries for the same document.
Browsers tend to handle the popstate event differently on page load. Chrome (prior to v34) and Safari always emit a popstate event on page load, but Firefox doesn't.
Syntax window.onpopstate = funcRef; //funcRef is a handler function.
简单来说,当同一个页面在历史记录间切换时,就会派发popstate事件。
正常情况下,用户点击后退按钮或者调用history.back() or history.go(),页面根本没有处理事件的机会,因为这些操作会使得页面reload,所以popstate只在不会让浏览器页面刷新的历史记录之间切换才能触发,这些历史记录一般由pushState/replaceState或者是由hash锚点等操作产生,并且在事件的句柄中可以访问state对象的引用副本!