Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the browser back button take you back through AJAX calls?

I have a page with a lot of dynamically generated check boxes on it. As the users click these check boxes a lot of content on the page changes dynamically via ajax. The end users are complaining that after hitting submit and then hitting the back button to change something, their selections are blown away and they have to do it all over again.

I have seen a few sites (gmail, facebook, etc...) use the hash symbol in the URL to hack the back button so that it performs AJAX calls instead of going back to the previous full page request. I would like to do this to modify the URL before the page submits so that hitting the back button will load their previously selected fields.

For instance:

In Gmail if I am viewing my inbox then my URL looks like this: https://mail.google.com/mail/?shva=1#inbox

Then if I click "Sent Mail" an AJAX call is performed and my URL is modified to look like this: https://mail.google.com/mail/?shva=1#sent

I really like this behavior and want to duplicate it. How is this accomplished?

  1. Do your links actually trigger any javascript or do they just link to the URL with the appropriate hash symbol information?

  2. How do you read in the hash symbol info in javascript?

  3. How does this type of navigation affect search engines? Would a search engine know that two URLS that are the same except for the information after the hash are actually different URLs and index them as such?

  4. What are some other pros and cons of this technique that I should take into consideration?

NOTE: I am using C# with ASP.NET Web Forms and ASP.NET MVC 3.0 in case that matters at all.

like image 948
Chev Avatar asked May 09 '11 22:05

Chev


1 Answers

To manipulate hashtags, look at location.hash (javascript).

You'll also be interested in the new push/pop state stuff in HTML 5. https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history.

github has done some pretty cool things with this. Check out their blog entry on their tree slider feature at https://github.com/blog/760-the-tree-slider.

There's also the jQuery history plugin at http://tkyk.github.com/jquery-history-plugin/. (EDIT: I see Joe beat me to this one).

like image 164
csano Avatar answered Sep 24 '22 04:09

csano