history.pushState & 有什么区别?位置.hash? [英] What are the differences between history.pushState & location.hash?
本文介绍了history.pushState & 有什么区别?位置.hash?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用 window.location.hash
或 history.pushState
更新 URL.
I want to update the URL using window.location.hash
or history.pushState
.
每种方法的区别和优点是什么?
What are the differences and advantages of each method?
推荐答案
location.hash
比 history.pushState
方法.
pushState
方法的优点是可以将状态绑定到历史条目.
The advantage of the pushState
method is that you can bind a state to the history entry.
如果你不需要这个状态对象,我推荐使用 location.hash
属性,以更好地兼容旧浏览器.
If you don't need this state object, I recommend to use the location.hash
property, to have better compatibility with older browsers.
location.hash = 'new-hash';
console.log(history.state); // null or undefined
history.pushState({extraData: "some state info"}, '', 'new-hash'); //<---
console.log(history.state); // [object Object] = {"extraData": "some state info"}
这篇关于history.pushState & 有什么区别?位置.hash?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文