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 "";
}