Alex Rivera | Logout

Listening to browser back button when using ajax and saving the html data as an object. Good or bad?

Asked 2012-09-10T21:42:18.463
9

I am creating a script for listening to browser back button and loading the previous page. I am new to jquery and javascript (a php person). Before re inventing this, I have searched the whole web for a library. But as I am using a lot of parameters in my ajax links, I cant use those libraries. I admit its my mistake, because I dont know how to use such complex systems. So I am thinking if creating a system as follows.

// get the contents of a particular div and save as an object/associative array 
// { hash : pageNumber, html : content}
function save_history(div){
    var content = $(div).html();
    // increment the page number and add hash tags to the URL
}   

// Listen to the browser hash value change
$(window).bind('hashchange', function () {
    hash = window.location.hash;
    if(hashValue){
        load_history(hashValue);
    }   
}); 

// Load data from history
function load_history(id){
    // fetch the content based on the hashvalue
    $(div).html(content);
}

Is there any problem in using this? Will this make the pages unresponsive or crash the browser, when a lot of contents are saved as objects? I dont want to waste my time if it causes such problems.

Edit
Report

1 Answer

1

Seems like pjax is the library you're looking for: https://github.com/defunkt/jquery-pjax

answered 2012-09-15T22:21:55.010

Your Answer