Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 601
Mic Avatar asked Sep 10 '12 21:09

Mic


1 Answers

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

like image 110
Vanusa Avatar answered Oct 07 '22 00:10

Vanusa