Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling back/fwd key events for an Ajax Application

I have an application which works heavily on AJAX. However I want to have navigation functionalities in it. To spoof the url, I am changing the location.hash, to generate URL. But if I use back/fwd, only the url changes, but page wont reload. How can I override the hstory.back to reload the page.

like image 841
Rakesh Avatar asked Dec 22 '22 12:12

Rakesh


1 Answers

I don't know of any other way than continuous polling to implement this behaviour. An implementation might look like this:

var lastHash = '';

function pollHash() {
    if(lastHash !== location.hash) {
        lastHash = location.hash;
        // hash has changed, so do stuff:
        alert(lastHash);
    }
}

setInterval(pollHash, 100);
like image 188
Christoph Avatar answered Dec 25 '22 02:12

Christoph