Naterade
Naterade

Reputation: 2675

How to implement Ajax back button simulation without href

So my entire site runs off of an index.php page and the content loads dynamically with many AJAX calls depending on what the user wants to do. I want to use the back button but currently I cannot. I have read some great tutorials and scoured over some great Stackoverflow questions regarding this. My problem is this. All the tutorials I'm seeing, including the one I'm leaning to towards, Jquery BBQ, seem to use href in all the tutorials but I don't use anchor tags at all.

A typical function of mine....to say load a comment.

$('.comments').live('click', function() {
    var id = this.id;
    // pass the unique id of the class to a php function, return result
});

I know I will use the id to keep track of the hash in the url, but I'm not sure how to do this when all I'm finding is anchor tag examples. Can someone point me in the right direction of a good tutorial, thanks!

Upvotes: 1

Views: 637

Answers (1)

Jasper
Jasper

Reputation: 76003

You can use the hash property of the window.location object:

$('.comments').live('click', function() {
    var id = this.id;
    window.location.hash = id;
    // pass the unique id of the class to a php function, return result
});

And then if a user refreshed the page or deep-links into the site:

if (typeof(window.locaion.hash) != 'undefined') {
    //now you know there is a hash and you can trigger a click on the corresponding element
    $('#' + window.locaion.hash).trigger('click');
}

Upvotes: 2

Related Questions