toGMTString is a function, you need to call.
Replace expiryDate.toGMTString to expiryDate.toGMTString()
const Cookie = {
get: function (name) {
const nameEq = name + "=";
const ca = document.cookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1, c.length);
}
if (c.indexOf(nameEq) == 0) {
return c.substring(nameEq.length, c.length);
}
}
return null;
},
set: function (name, value, hours) {
const expires = "";
if (hours) {
let date = new Date();
date.setTime(date.getTime() + (hours * 60 * 60 * 1000));
const expires = "; expires=" + date.toGMTString();
}
document.cookie = name + "=" + value + expires + "; path=/";
}
};
You can change hours
to whatever and of course
this one date.setTime(date.getTime() + (hours * 60 * 60 * 1000));
to the corresponding formula
Usage`
Set: Cookie.set('myemail', 'test@gmail.com', 1460) // 1460 is 2 months in hours
Get: Cookie.get('myemail')
const email = document.getElementsByClassName('name')[0].textContent.split(' ')[1]; Cookie.set('myemail', email, 1460);