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"
1//write
2document.cookie = "username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC";
3//read
4let x = document.cookie
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<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 }
1import cookies from 'js-cookie'
2
3export const getUserFromCookie = () => {
4 const cookie = cookies.get('auth')
5 if (!cookie) {
6 return
7 }
8 return JSON.parse(cookie)
9}
10
11export const setUserCookie = (user) => {
12 cookies.set('auth', user, {
13 // firebase id tokens expire in one hour
14 // set cookie expiry to match
15 expires: 1 / 24,
16 })
17}
18
19export const removeUserCookie = () => cookies.remove('auth')