浏览器历史就像一堆卡片,以下所示:html
在HTML4中,咱们已经可使用window.history对象来控制历史记录的跳转,可使用的方法包括:git
方法 | 描述 |
---|---|
back() | 加载 history 列表中的前一个 URL。 |
forward() | 加载 history 列表中的下一个 URL。 |
go() | 加载 history 列表中的某个具体页面。 |
HTML5引进了history.pushState()方法和history.replaceState()方法,它们容许你逐条地添加和修改历史记录条目。这些方法能够协同window.onpopstate
事件一块儿工做。github
方法 | 描述 |
---|---|
pushState() | 有三个参数:一个状态对象、一个标题(如今会被忽略),一个可选的URL地址 |
replaceState() | 操做相似于history.pushState(),不一样之处在于replaceState()方法会修改当前历史记录条目而并不是建立新的条目。 |
popstate事件 | 每当激活的历史记录发生变化时,都会触发popstate事件。 |
若是要作浏览器兼容,能够下载History.js解决。下面作个简单的demo:ajax
<h1 id="number">1</h1> <a id="forward" href="?num=2">Go Forward</a>
var link = document.getElementById('forward'); var num = document.getElementById('number'); link.addEventListener('click', function(e){ e.preventDefault(); var myNum = parseInt(num.innerHTML, 10); num.innerHTML = ++myNum; history.pushState({count:myNum}, null, '?num='+myNum); document.title = 'Number'+myNum; });
点击按钮的时候,上面的URL在改变,但点击浏览器的后退按钮的时候,上面的内容是不变的。数组
接下来,咱们给popstate加个监听器,监听数字。数字内容也就会随着改变了。浏览器
addEventListener('popstate', function(e){ if(e.state && e.state.count) { num.innerHTML = e.state.count; document.title = 'Number'+e.state.count; }else { num.innerHTML = 1; document.title = 1; } });
PAJX就是pushState+Ajax。同时使用这二者,只需更新页面的一小部分和URL,页面不须要从新加载,这样就能极大加快访问速度。缓存
创建一个单页程序的方法不少,最简单的就是使用一个路由。app
var routes = []; //创建route对象,插入到数组routes中 function addRoute(route, callback, scope) { var routeObj = { route: route, callback: callback, scope, scope }; routes.push(routeObj); } //匹配符合要求的route,并执行回调函数 function handleRoute(path, noHistory) { var len = routes.length, scope; for(var i=0; i<len; i++) { if(path.match(routes[i].route)) { //若是给定了做用域就用给的做用域 if(routes[i].scope) { scope = routes[i].scope; }else { scope = window; } //若是是来自popstate事件,就没必要再加入到栈中 if(!noHistory) { history.pushState({}, null, path); } routes[i].callback.apply(scope, [path]); return true; } } //没有route返回false return false; }
只需添加一个监听器就能让历史记录起做用。dom
window.addEventListener('popstate', function(e) { handleRoute(window.location.href, true); }); router = { handleRoute:handleRoute, addRoute:addRoute };
给页面上的a标签按钮添加事件函数
//拦截单击,简单的绑定页面上面的a标签的点击事件 document.addEventListener('click', function(e) { if(e.target.href) { if(router.handleRoute(e.target.href)) { e.preventDefault(); } } });
这里还作了点简单的缓存操做。点击后退按钮页面上的内容还能存在。
//缓存ajax的响应 var pageCache = {}; //格式化连接 function normalizeLink(path) { return path.match(/([a-z_]+\.html)/)[1]; } //执行ajax请求或使用缓存 function handlePage(path) { var href = normalizeLink(path); if(pageCache[href]) { document.querySelector('.content').innerHTML = pageCache[href]; } else { ajax.makeRequest(href.replace('.html', '_frag.html'), function(xhr) { document.querySelector('.content').innerHTML = xhr.responseText; pageCache[href] = xhr.responseText; }, this); } } router.addRoute(/[a-z_]+\.html/, handlePage);
代码是参照《HTML5触摸界面设计与开发》这本书的示例demo而写的。这本书的源码能够在这里获取到。
demo下载:
http://download.csdn.net/detail/loneleaf1/8730963
参考资料:
http://www.w3school.com.cn/jsref/dom_obj_history.asp History 对象
https://developer.mozilla.org/zh-CN/docs/DOM/Manipulating_the_browser_history 操纵浏览器的历史记录