Skip to content

Instantly share code, notes, and snippets.

@dancr13
Last active March 8, 2019 21:04
Show Gist options
  • Save dancr13/33665e04e933c8b46f9180f2adc1c43a to your computer and use it in GitHub Desktop.
Save dancr13/33665e04e933c8b46f9180f2adc1c43a to your computer and use it in GitHub Desktop.
Creating a cookie with a array
<script>
function getCookie(c_name) {
if (document.cookie.length > 0) {
c_start = document.cookie.indexOf(c_name + "=");
if (c_start != -1) {
c_start = c_start + c_name.length + 1;
c_end = document.cookie.indexOf(";", c_start);
if (c_end == -1) {
c_end = document.cookie.length;
}
return unescape(document.cookie.substring(c_start, c_end));
}
}
return "";
}
function cookieInit() {
document.cookie = "dup=1; expires=Thu, 18 Dec 2040 12:00:00 UTC; path=/";
pagesCookie = getCookie("pages");
if (pagesCookie == '') {
var pages = [window.location.href];
document.cookie = "pages="+JSON.stringify(pages)+ "; expires=Thu, 18 Dec 2040 12:00:00 UTC; path=/";
}
else
{
var json_str, pagesArr;
json_str = getCookie("pages");
pagesArr = JSON.parse(json_str);
currentUrl = window.location.href;
if(!pagesArr.includes(currentUrl)){
pagesArr.push(currentUrl);
document.cookie = "pages="+JSON.stringify(pagesArr)+ "; expires=Thu, 18 Dec 2040 12:00:00 UTC; path=/";
}
}
}
cookieInit();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment