function preventBack (currentUrl) {
history.pushState(null, null, currentUrl);
window.addEventListener('popstate', function () {
history.pushState(null, null, currentUrl);
});
}
pushState和popState的参数都有三个,分别是state、title、href。
state是一个由 pushState()创建的、与历史纪录相关的JS对象。当用户定向到一个新的状态时,会触发popstate事件。事件的state属性包含了历史纪录的state对象。title与浏览器有关,可以不设置,href是新地址。
pushState和popState可用做到,无刷新改变地址栏,并且在历史记录中添加新的纪录,这样子就方便了控制浏览器页面的回退和前进。
2513

被折叠的 条评论
为什么被折叠?



