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

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

    Pushstate is the future. It's better because:

    1. It looks cleaner.
    2. On revisit of a deep link you can actually surface real serverside data to support things like SEO and Facebook Open Graph (both send spiders to scrape the html of your page).
    3. Servers don't have access to hash tags data so you don't see it in your server logs so it helps some with analytics.
    4. It helps fix hash tag issues. For example I had an Nginx rewrite to redirect users visiting my app to the same https url. It works in all browsers but Safari which will redirect you to just the domain without the hash (so annoying)!
    5. You can actually use hash tag for what is was meant for, deep linking to sections of long pages.
    6. You can fallback to using real HTTP backend requests for browser that don't support push state OR you can fallback to using hash tag. Both require extra implementation but are easily doable with a little work.

    See this talk from Github designer for more: http://warpspire.com/talks/responsive/

提交回复
热议问题