问题描述:

I want to create recently viewed section displaying images of the posts the user has viewed. I want to create a cookie that will store the ID's of the visited pages and then I guess I will have to use AJAX to display the images of the posts. I am using JQuery Cookie Plugin. But so far my cookie is storing only one ID instead of array of a few.

Each posts is contained withing a div with id="product-id" that contains the post ID - <div id="product-id" data-id="<?php echo get_the_ID(); ?>">

Here is my code:

 $( document ).ready(function() {

///// SETTING THE COOKIE /////

Cookies.set('history', $('#product-id').data('id'), { expires: 30, path: '/' });

///// MAIN JQUERY FUNCTION //////////

function checkHistory() {

var history = Cookies.get('history');

var cookie = $('#product-id').data('id');

var htmlContent = '';

if (history != "" && history!=null) {

var insert = true;

var sp = history.toString().split(",");

for ( var i = sp.length - 1; i >= 0; i--) {

if (sp[i] == cookie) {

insert = false;

}

}

if (insert) {

sp.push(cookie);

}

Cookies.set('history', sp.toString(), { expires: 30, path: '/' });

} else {

var stack = new Array();

stack.push(cookie);

Cookies.set('history', stack.toString(), { expires: 30, path: '/' });

}

}

});

Please note that I am very new to JQuery. Any explanation will be deeply appreciated.

相关阅读:
Top