Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Facebook change the URL in the browser's address bar without page reload or use of # or?

A couple of months ago I saw a new feature deployed on vkontakte.ru (russian FB): when going between users pages, no browser reload happens. Instead, the new page gets preloaded, and then displayed. The music player that’s on the page (it’s anchored on top of the footer) keeps playing, uninterrupted.

However, the url in browser address bar changes, and not by means of a hash tag or a query string.

So, my page is vk.com/myPage... I click a friend, his page preloads, then gets displayed at which time the URL in browser address bar changes to vk.com/myfriendJoe, and no browser reload happens.

Note that it only works with the newest browsers like later versions of Safari and Chrome, for others like Firefox 3.6 it implements a fallback that uses hashtags and query strings.

I did some research and found window.history.pushState, but it only seems to work with query strings.

Any ideas would be greatly appreciated. Thank you.

like image 776
toli Avatar asked Jul 22 '11 17:07

toli


People also ask

How can I change my URL without reloading the page?

history. pushState({page: "another"}, "another page", "example. html"); This will change the URL, but not reload the page.

Which method is used to change the address in address bar of the browser?

HTML5 History API allows browsers to change the URL in browser address bar without reloading or refreshing the page using pushState function. The pushState method works similar to window.

How do I keep the same URL in the address bar?

Buy a domain name and then make a redirection (maybe you can ask for this to your hosting provider). Fastest way: use an iframe.


2 Answers

That's the new history API defined by HTML5. You can see another demo here. You're quite right that it's pushState that does it, but it doesn't just work with query strings (as that page demonstrates, it's using rooted relative URLs like /history/first and /history/second).

like image 155
T.J. Crowder Avatar answered Oct 06 '22 20:10

T.J. Crowder


If you're using a modern HTML5 compliant browser, you can use the history.pushState() API.

More here: https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history

like image 38
Mrchief Avatar answered Oct 06 '22 20:10

Mrchief