window.history的简单回顾javascript
window.history.back(); window.history.forward(); windiw.history.go(-1);//至关于back() window.history.go(1);//至关于forwar() window.history.go(0);//至关于刷新 window.history.length;//查看历史记录栈中一共有多少个记录
对历史记录点进行修改html
history.pushState(state, title, url)java
>接受三个参数: >state:状态对象,记录历史记录点的额外对象,可为null >title:页面标题,目前因此浏览器都不支持,须要的话能够用document.title来设置 >url:新的网址,必须同域,浏览器地址栏会显示这个网址 window.history.pushState(null, '', 'a.html'); //页面不刷新,只是改变history对象,地址栏会改变 window.history.pushState(null, '', '#aaa'); >//url参数带了hash值并不会触发hashchange事件
history.replaceState(state, title, url)web
window.history.replaceState({a:1}, '', 'b.html')
history.state属性ajax
window.history.state //{a:1}
监听历史记录浏览器
window.onhashchange = function(){ console.log(location.hash) };//hashchange事件必须绑定再widnow对象上
if(('onhashchange' in window) && ((typeof document.documentMode === 'undefined') || document.documentMode == 8)) { //在IE8以ie7的模式运行时,window下存在onhashchange这个方法,可是永远不会触发这个事件 //不能用 typeof window.onhashchange === ‘undefined’ 来检测,由于不少支持onhashchange的浏览器下,其初始值就是undefined window.onhashchange = function() { console.log(window.location.hash); }; } else {//不支持onhashchange用定时器判断hash是否改变 var location = window.location; var oldHash = location.hash; setInterval(function() { var newHash = location.hash; if(newHash === oldHash) { console.log(location.hash); } }) }
window.oppopstate = function(event) { console.log(event.state); }//event.state的值是history.state的值
简单应用:无刷新页面,改变url,产生历史记录网站
参考文章:url
http://www.impng.com/web-dev/hashchange-event-and-onhashchange.htmlcode