0

I'm working in an online catalog that is composed by a page (let's call it a) with a div .contenido-catalogo which gets the products through ajax. Those products have a link to another page (let's call it b). The idea is to save page a's .contenido-catalogo in content variable before moving to page b and when the back button of the browser is clicked retrieve the content of the div previously saved. This is my code:

$(document).ready( function () {

    var content;

    if (getCookie('foo') == 'bar') {
        $('#contenido-catalogo').html(content);
        setCookie('foo', '');
    };

    $('body').on('click', '.product-link', function() {
        setCookie('foo', 'bar');
        content = $('#contenido-catalogo').html();
    });



    function setCookie(cname, cvalue) {

        document.cookie = cname + "=" + cvalue + ';';
    }

    function getCookie(cname) {
        var name = cname + "=";
        var ca = document.cookie.split(';');
        for(var i=0; i<ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1);
            if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
        }
        return "";
    }
chino
  • 19
  • 1
  • 6
  • 4
    i suggest you use local storage for that.save the content there and retrieve it again if needed. but i think you also need to manually clean the localstorage to avoid confusion. – guradio Sep 14 '15 at 09:05
  • you mean in a cookie right? – chino Sep 14 '15 at 09:08
  • https://developer.mozilla.org/en/docs/Web/API/Window/localStorage – pawel Sep 14 '15 at 09:13
  • http://stackoverflow.com/questions/2010892/storing-objects-in-html5-localstorage here is a good way of storing in localstorage – guradio Sep 14 '15 at 09:15
  • hmm, it doesn't seem to work... do you know if browsers back button triggers `$(document).ready` – chino Sep 14 '15 at 09:48

0 Answers0