如今基本都是单页面应用,如今单页面应用可以模拟多页面应用的效果,归功于其前端路由机制。如今前端路由有两种形式:css
如: xxx.xx.com/#/hash, 这种方式能够实现刷新跳转,不须要后端进行配合,主要是得益于haschange事件,当锚点哈希改变时,就会调用haschange函数,html
(function(window) {
// 若是浏览器原生支持该事件,则退出
if ( "onhashchange" in window.document.body ) { return; }
var location = window.location,
oldURL = location.href,
oldHash = location.hash;
// 每隔100ms检测一下location.hash是否发生变化
setInterval(function() {
var newURL = location.href,
newHash = location.hash;
// 若是hash发生了变化,且绑定了处理函数...
if ( newHash != oldHash && typeof window.onhashchange === "function" ) {
// execute the handler
window.onhashchange({
type: "hashchange",
oldURL: oldURL,
newURL: newURL
});
oldURL = newURL;
oldHash = newHash;
}
}, 100);
})(window);
复制代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<ul>
<li><a href="#/css">css</a></li>
<li><a href="#/js">js</a></li>
<li><a href="#/node">node</a></li>
</ul>
<div class="wrapper">
当前选择的是:<span></span>
</div>
</body>
<script>
window.onhashchange = function (e) {
console.log(e);
document.querySelector('.wrapper span').innerText = location.hash.split('/')[1]
}
</script>
</html>
复制代码
如:xxx.xx.com/hash 这种模式须要后端路由配合使用。HTML5的History API为浏览器的全局history对象增长的扩展方法。通常用来解决ajax请求没法经过回退按钮回到请求前状态的问题前端
在HTML4中,已经支持window.history对象来控制页面历史记录跳转,经常使用的方法包括:node
在HTML5中,window.history对象获得了扩展,新增的API包括:ajax
history.pushState({page: 1}, null, '?page=1');
history.pushState({page: 2}, null, '?page=2');
history.back(); //浏览器后退
window.addEventListener('popstate', function(e) {
//在popstate事件触发后,事件对象event保存了当前浏览器历史记录的状态.
//e.state保存了pushState添加的state的引用
console.log(e.state); //输出 {page: 1}
});
复制代码