0

I'm using ajax in my website and there is an ajax function that controls all links.

something like this:

function sendGet(url,dataform){
    $.post(url,{_ajax:1},function(data){
        $(dataform).html(data);
        window.location.hash = url;
    });
    return false;
}

and set hash with url.

alse I have this code in document load:

    var load_hash = window.setInterval(function(){
        if(window.location.hash) sendGet(window.location.hash.substr(1),"#include_content");
        clearInterval(load_hash);
    },1);

when back button is pressing I want to load the url in hash.

BUT most of the time it is not working. it works sometimes when I refresh the page.

am I doing it right?

if not, is there any other way to do this?

Pooya
  • 1,508
  • 3
  • 17
  • 38

1 Answers1

1

You can add an event to the window object that listens to hashchange:

$(window).bind('hashchange', function() {
    sendGet(window.location.hash.substr(1),"#include_content");
});

That should work if you click the backbutton to load the correct page.

putvande
  • 15,068
  • 3
  • 34
  • 50