通过window.history下的pustState函数把数据写入到历史记录里面
1.首先进行编码
let encode = decodeURI(val); //进行解码,防止浏览器自动编码,val:存进历史记录的数据
2.判断浏览器是否兼容window.history并写入到pustState
if(window.history && window.history.pushState){
window.history.pushState(encode,'历史','#abc='+encode); //三个参数 :数据 标题(都没实现) 地址(可选)
}
3.监听前进,后退的按钮(第一种方法)
window.onpopstate = function(e) {
if(e.state){
con.innerHTML = data[e.state];//e.state:历史的记录数据
}
}
4. 监听has变化(第二种方法)
window.onhashchange = function(){
console.log(window.location.hash);//打印出的值为地址栏的has值
}