Search code examples
javascriptasp.netupdatepanel

Browser's backbutton trouble when using ajax calls


I have a list with search results that is fetched through ajax (to be precise: the microsoft updatepanel). The problem is that I cannot use the backbutton of any webbrowser to navigate back to previous lists that I fetched through ajax. Do you have an idea?

Thanks


Solution

  • You need to make all the ajax calls update the window.location.hash.

    function getAjaxResource(id) {
      // some ajax stuff
      window.location.hash = 'resource=' + id;
    }
    

    Then you need to add a watcher to the hash with javascript's setInterval function.

    var hash = window.location.hash;
    window.setInterval(function () {
      if (window.location.hash != hash) {
        hash = window.location.hash;
        getAjaxResource(hash.replace('resource=',''));
      }
    },100);
    

    The hash changes every time the user clicks back/forward and the watcher will pick that change up.