window.history.back(); // 后退 window.history.forward(); // 前进 window.history.go(-1); // -1:后退一个页面 1:前进一个页面 0:刷新页面 // 1给页面添加一条浏览记录,使url后增加?page=1,并能通过history.state获取JSON{page: 1} history.pushState({page: 1}, "title 1", "?page=1"); // 2修改当前浏览记录的两种方法 history.replaceState({page: 1}, "title 1", "?page=1");
location.replace('http://baidu.com') // 3监听页面浏览记录 //(1.无页面跳转时不会调用,如history.go(200)不会打印log) //(2.页面未pushState时,state为null) window.onpopstate = function(event) {console.log("state: " + JSON.stringify(event.state));}; // 测试代码 window.onpopstate = function(event) { console.log("state: " + JSON.stringify(event.state)); }; history.pushState({page: 1}, "title 1", "?page=1"); history.pushState({page: 2}, "title 2", "?page=2"); history.replaceState({page: 3}, "title 3", "?page=3"); history.back(); history.back(); history.go(2);