Javascript – Why does Facebook use hash in the URL…sometimes

javascriptweb-development

I've noticed that Facebook SOMETIMES uses a hash in the url. For example, when I'm on my home page, and then I click my profile page, the URL quickly changes to:

facebook.com/#_________

followed by some other stuff.

before quickly loading to:

facebook.com/profileName

It doesn't always do this. What's going on here?

Best Answer

The "#" allows you to target something in a web-page. as in

...
<div id="foo"></div>
...

to target foo div in a page you can just do #foo

That being said there is no need to reload the page if you just change the # in the url, and facebook uses this as a hack. What they do is they just change the hash 'which makes the url bookmarkable' at the same time they can load up assets using XHR and the page doesn't reloads itself providing a seamless experience to the user on the other hand it doesn't make as many HTTP requests to the server for CSS / HTML / JS files (despite of being cached the validation is required) and hence reducing the amount of content they have to render and number of HTTP requests to their server, they also can increase the performance and reduce latency of the website doing so.

There is though a better way to do this which is documented in the HTML 5 History API( Which you should read perhaps ) Which allows you to change the URL in the address bar by pushing history states.

Related Topic