ajax能無刷新更新數據,但是不能更新url
HTML5的新API: window.history.pushState, window.history.replaceState
用戶操作history,點擊前進后退按鈕會觸發popstate事件。
這些方法可以協同window.onpopstate
事件一起工作。
改變url的demo
本頁是foo.html,url改變成bar.html,內容卻不變
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <button onclick="changeUrl()">按鈕</button> <script> function changeUrl() { var stateObj = {foo: 'bar'}; window.history.pushState(stateObj, 'page 2', 'bar.html'); // 這將讓瀏覽器的地址欄顯示http://mozilla.org/bar.html,但不會加載bar.html頁面也不會檢查bar.html是否存在。 } </script> </body> </html>