What are the differences between history.pushState & location.hash?

前端 未结 8 1371
既然无缘
既然无缘 2020-11-30 03:29

I\'m trying to update the URL using window.location.hash or history.pushState.

What are the differences and advantages of each method?

8条回答
  •  抹茶落季
    2020-11-30 03:42

    Currently, pushState is supported by all modern browsers. Therefore pushState is better than location.hash, but it is a HTML5 feature.

    So, location.hash is not dead, in fact it will be around for a long, long time.

    A good way to use this is with a lib that supports pushState, but also gracefully degrades to using location.hash.
    Example - https://github.com/browserstate/history.js

    Moreover location.hash will still be useful for jumping to named anchors. pushState will be a tremendous help in building web apps. I look forward to using it.

提交回复
热议问题