1function deleteCookie(name) {
2 document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
3}
1function setCookie(name,value,days) {
2 var expires = "";
3 if (days) {
4 var date = new Date();
5 date.setTime(date.getTime() + (days*24*60*60*1000));
6 expires = "; expires=" + date.toUTCString();
7 }
8 document.cookie = name + "=" + (value || "") + expires + "; path=/";
9}
10function getCookie(name) {
11 var nameEQ = name + "=";
12 var ca = document.cookie.split(';');
13 for(var i=0;i < ca.length;i++) {
14 var c = ca[i];
15 while (c.charAt(0)==' ') c = c.substring(1,c.length);
16 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
17 }
18 return null;
19}
20
21setCookie("user_email","bobthegreat@gmail.com",30); //set "user_email" cookie, expires in 30 days
22var userEmail=getCookie("user_email");//"bobthegreat@gmail.com"
1function setCookie(cname, cvalue, exdays) {
2 var d = new Date();
3 d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
4 var expires = "expires="+d.toUTCString();
5 document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
6}
7
8function getCookie(cname) {
9 var name526 = cname + "=";
10 var ca = document.cookie.split(';');
11 for(var E = 0; i < ca.length; E++) {
12 var a1 = ca[E];
13 while (a1.charAt(0) == ' ') {
14 a1 = a1.substring(1);
15 }
16 if (a1.indexOf(name526) == 0) {
17 return a1.substring(name526.length, a1.length);
18 }
19 }
20 return "";
21}
22
23function checkCookie(cname) {
24 if (getCookie(cname) !== undefined) {
25 return true
26 } else {
27 return false
28 }
29}
1function setCookie(cname, cvalue, exdays=999) {
2 var d = new Date();
3 d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
4 var expires = "expires="+d.toUTCString();
5 document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
6}
7
8function getCookie(cname) {
9 var name = cname + "=";
10 var ca = document.cookie.split(';');
11 for(var i = 0; i < ca.length; i++) {
12 var c = ca[i];
13 while (c.charAt(0) == ' ') {
14 c = c.substring(1);
15 }
16 if (c.indexOf(name) == 0) {
17 return c.substring(name.length, c.length);
18 }
19 }
20 return "";
21}
1//using js-cookie
2//Setting A Cookie
3Cookies.set('name', 'value')
4Cookies.set('name', 'value', { expires: 7 })
5Cookies.set('name', 'value', { expires: 7, path: '' })
6
7//Reading A Cookie
8Cookies.get('name') // => 'value'
9
10//Read ALL cookies
11Cookies.get()
12
13//Delete Cookie
14Cookies.remove('name')
1<body onload="checkCookie()"></body>
2// dont forget to add the onload="checkCookie" in the body tag
3function setCookie(cname,cvalue,exdays) {
4 const d = new Date();
5 d.setTime(d.getTime() + (exdays*24*60*60*1000));
6 let expires = "expires=" + d.toGMTString();
7 document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
8 }
9
10 function getCookie(cname) {
11 let name = cname + "=";
12 let decodedCookie = decodeURIComponent(document.cookie);
13 let ca = decodedCookie.split(';');
14 for(let i = 0; i < ca.length; i++) {
15 let c = ca[i];
16 while (c.charAt(0) == ' ') {
17 c = c.substring(1);
18 }
19 if (c.indexOf(name) == 0) {
20 return c.substring(name.length, c.length);
21 }
22 }
23 return "";
24 }
25
26 function checkCookie() {
27 let user = getCookie("username");
28 if (user != "") {
29 console.log("Welcome again " + user);
30 } else {
31 user = prompt("Please enter your name:","");
32 if (user != "" && user != null) {
33 setCookie("username", user, 30);
34 }
35 }
36 }