1

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); 

enter image description here

and i want my array to keep all the id's and not just one.

5
  • 1
    Get the cookie to load visited post IDs into arr, then check if you need to push current ID. Commented May 29, 2020 at 15:51
  • it's working, but the array will always have just one ID(the current one), but i need all of them Commented May 29, 2020 at 15:55
  • 1
    What he meant is that when you initialize the "arr" variable, it should contain the former IDs. If you initialize the "arr" variable each time and push the new ID you will only get the new ID in the array. Commented May 29, 2020 at 16:10
  • aaa, and how can i initialize the arr variable with the former IDs?=) Commented May 29, 2020 at 16:24
  • i dont really know how to get the visited post IDs into array Commented May 29, 2020 at 17:07

1 Answer 1

1

Follow these steps:

  • Don't create your arr variable until you need it
  • Add a function to read cookie and return contents as JSON
  • On page load, read the cookie, update your array, then save new contents

Final code should see like this:

 let post_id = my_script_vars.postID; // this is a variable with some id's

 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=/";

}
// Function to read cookie and return as JSON
function getCookie(name) {
    let a = `; ${document.cookie}`.match(`;\\s*${name}=([^;]+)`);
    return a ? JSON.parse(a[1]) : [];
}

// Be sure to execute after DOM is loaded
window.addEventListener('load', function() {
    // Get saved IDs
    let arr = getCookie('id_film');
    index = arr.indexOf(post_id);
    if (index == -1) {
        // if the id is not in the array the push it
        arr.push(post_id);
    }
    setCookie("id_film",JSON.stringify(arr),30);
});
Sign up to request clarification or add additional context in comments.

1 Comment

thank u so much, finally i understand how this works, and its not so hard after all, thankssss=)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.