I want to update the cookie from my WP page with the post id's that the user is visiting. But my problem is that each time the value from the array is deleted and the array contains always just on id, and the next id's are just overwited.
let post_id = my_script_vars.postID; // this is a variable with some id's
let arr = [] // i create an array
function setCookie(name,value,days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
index = arr.indexOf(post_id);
if (index == -1) { // if the id is not in the array the push it
arr.push(post_id);
} else { // if it is in array then keep all of them
arr.splice(index, 1);
}
setCookie("id_film",JSON.stringify(arr),30);
and i want my array to keep all the id's and not just one.