How can I use #-URLs in a single-page application?

前端 未结 1 1021
执念已碎
执念已碎 2021-02-04 08:04

This article makes a pretty convincing argument that because URLs are long-lived (they get bookmarked and passed around), they should be meaningful, and that using the hash for

相关标签:
1条回答
  • 2021-02-04 08:07

    The hash value of a URL never caused and entire re-load of page. Not in HTML4 and before that. A hash value has always been an internal link, and therefore it can be used perfectly (take a look at twitter for example). Of course, when you refresh the page, you will reload the page. But that is obvious.

    With JavaScript you can actually read this hash value (see also this question/answer: How can you check for a #hash in a URL using JavaScript?) using window.location.hash

    Using a more recent browser, you can also detect a hash change, which is useful if users actually change the URL: On - window.location.hash - Change?

    But when you, as the website, change the URL you don't need read this, you already know because you just changed it.

    This way, using hashes, people can exchange the URLs, and you can actually read which URL they are requesting, and therefore it should work perfectly.

    0 讨论(0)
提交回复
热议问题