This is commonly done by either using the latest HTML5
history API, or, for old browsers, changing the hash in the URL. Have a look at this article: http://dev.opera.com/articles/view/introducing-the-html5-history-api/.
Whenever some user action prompts a new page, instead of loading the new page (with a new URL), the page retains the common properties (like say, the header, footer, or say, in a eCommerce site, the shopping cart widget), and the remaining contents are fetched asynchronously through Ajax
. Once the new content is fetched, the UI is changed accordingly, and a new state
pushed into the History stack, and changing the URL accordingly. Through the data
attribute used here as a dictionary, you store information about what to show when the user navigates forward/backward and arrives at that page again. Something like this:
history.pushState({mydata1: data, mydata2: anotherData},"newTitle",newURL);
So when the user presses the Back button of the browser, that entry is simply popped off the history stack, and from the data attributes of the new entry in the top of the stack, the new page is shown. The current page can be updated as well:
history.replaceState({mydata1: data, mydata2: anotherData}, "anotherTitle",newURL);
All information regarding the current page can be obtained from state
:
myState = history.state;
If you have observed, Facebook does the same thing. When you click the Home button, or go a friend's profile, no new page is loaded. The notification ticker on the right remains as it is (instead of loading again for the new page), if you have any chat box opened, it stays as it is. This is because the page doesn't reload to the new page (in which case, all widgets, including chat and ticker should reload as well), only the URL is changed dynamically, and the new content is fetched and displayed through Ajax.
If you view Facebook in IE8
, which doesn't support History
API, you will find it still works, but the URL change behavior is different. It is because they fall back on the good old way of changing URLs by changing the hash.