// Helper for setCookie() - gives the top level directory - eg /myapp function cookiePath() { const a = location.pathname.split('/'); if (a.length < 1) return '/'; return '/' + a[1]; } function setCookie(name, value, days) { if (name == '') return; if (value === undefined) value = ''; let expires = ''; if (days) { let date = new Date(); date.setDate(date.getDate() + days); expires = '; expires=' + date.toUTCString(); } document.cookie = name + '=' + value + expires + '; path=' + cookiePath(); } function getCookie(name) { if (name == '') return ''; const nameEq = name + '='; const pairs = document.cookie.split(';'); for (let i = 0; i < pairs.length; i++) { const pair = pairs[i].trim(); if (pair.indexOf(nameEq) == 0) return pair.substring(nameEq.length, pair.length); } return ''; } function removeCookie(name) { document.cookie = name + '=; Max-Age=-99999999; path=' + cookiePath(); } function exampleUse() { setCookie('test', 'apple'); console.log('cookie is ' + getCookie('test')); }More info https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies
Programming Tips - javaScript: easily set/get Cookies
Date: 2020apr9
Language: javaScript
Q. javaScript: easily set/get Cookies
A. Here are some straight ahead function that work for me