history方法与属性
history.pushState(对象,‘标题’,‘新增历史记录’) 不刷新页面的情况下 添加历史记录
history.replaceState(对象,‘标题’,‘替换的历史记录’)不是页面的情况下,替换当前历史记录
window.onpopstate 事件 ,但历史记录发生变化的时候触发。但是使用,pushState与replacestate不会触发。
pushState与replaceState传入的对象,保存在onpopstate事件下的state属性里。
例:
<p>I am index;</p>
<button class="btn">添加</button>
<button class="btn1">替换</button>
<script>
var obtn = document.getElementsByClassName('btn')[0];
obtn.onclick = function(){
history.pushState({name: 'cc'}, '', 'demo.html');//pushState replacestate不会刷新页面
}
var obtn1 = document.getElementsByClassName('btn1')[0];
obtn1.onclick = function(){
history.replaceState({name: 'amiee'},'', 'demo1.html');
}
window.onpopstate = function(e){
console.log('a');
console.log(e.state)
}
</script>