Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem with back button and AJAX

Newbie
 
Join Date: Nov 2007
Posts: 13
#1: Nov 27 '07
I have an Ajax application and I want to take control of the back button. I have implemented a Javascript, but it runs correctly only on Firefox. In IE7 I got some problems.

My script runs as follow:
- when I click from a tab to another of the same page I update the sub-content via Ajax and I change my url adding hash parameters (using window.location.hash).
- when I get an url with hash parameters I parse it and I send the correct sub-content to the page.

Everything manage correctly in Firefox. If I click on the browser back button I can always get the right url (with hash parameters), then I parse it and send the correct sub-content to the page.

In I.E. 7.0 if I click from a tab to another of the same page, the history browser doesn't push nothing. It contains always the first entry of my page (without hash parameters), while the browser bookmark got the correct url.

Could someone help me to find the problem?

acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Nov 27 '07

re: Problem with back button and AJAX


Can you post your code or a link to a test page that demonstrates the problem?
Newbie
 
Join Date: Nov 2007
Posts: 13
#3: Nov 28 '07

re: Problem with back button and AJAX


Here's my code:

Expand|Select|Wrap|Line Numbers
  1. var lastSavedHash = "";
  2.  
  3. /* function called when user click on tab AJAX button */
  4. function makeHash(mode)
  5. {
  6.     window.location.hash = mode;
  7.      lastSavedHash = window.location.hash;
  8.     return true;
  9. }
  10.  
  11. /* function who checks the hash values and send the correct sub-content to the page.*/
  12. function handleHash()
  13. {
  14.   if ( window.location.hash != lastSavedHash )
  15.   {
  16.     currentHash = window.location.hash;
  17.  
  18.     mode = lastSavedHash.replace("#", "");
  19.  
  20.     url = window.location.href;
  21.     querytext = url.split("/"); 
  22.  
  23.     if (mode != querytext[5])
  24.         <callAjaxUpdateSubContent(mode)>;
  25.   }
  26.     else
  27.         return '';
  28. }
  29.  
  30.  
  31. /* function called onload event and every x seconds  Call handleHash function*/
  32. function checkHash() {
  33.     handleHash();
  34.     setInterval("handleHash()", 200);
  35. }
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Nov 28 '07

re: Problem with back button and AJAX


How do you call makeHash()?
Newbie
 
Join Date: Nov 2007
Posts: 13
#5: Nov 28 '07

re: Problem with back button and AJAX


I call makeHash in the getAjaxContent function, i.e. every time the sub-content change after user click on a tab button.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#6: Nov 29 '07

re: Problem with back button and AJAX


This is an old article, but it might help explain IE's bug.
Newbie
 
Join Date: Jan 2008
Posts: 3
#7: Jan 29 '08

re: Problem with back button and AJAX


I've also same doubt...
Plz send the source code for currently working prog in Mozilla asap........
Plz help me...
I'll try and send the code to u...
Newbie
 
Join Date: Jan 2008
Posts: 3
#8: Jan 29 '08

re: Problem with back button and AJAX


can u post u r code as soon as possible...
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#9: Jan 29 '08

re: Problem with back button and AJAX


Quote:

Originally Posted by viswaj

can u post u r code as soon as possible...

Have you checked out the linked article (post #6)?

Please try to use full English when you post, not SMS-speak. Thanks!
Reply