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

前端 未结 8 1400
既然无缘
既然无缘 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 04:01

    I agree with the other answers, but here are a few arguments in favor of location.hash:

    • it works in every browser, including Internet ExploderTM
    • history.pushState is a developing standard, and the API may change in the future
    • if the users opens a link in a new window/tab, a hash-URL makes sure there is no server request needed to load the page (if the correct caching headers are set)
    • The server configuration is easy, since all the server ever sees is the URL without hash-part

    edit: I forgot one

    • with hashtags, you can use real links (a href). So you don't have to set up click listeners, which improves performance and reduces code size.

提交回复
热议问题