Reputation: 23
I have difficulties setting the cookie time to reset after 1 week. Does anyone know how to achieve this? I am using the following code in my Shopify theme. I can now only select to show it once or show it always in my theme settings.
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}
Looking forward to your replies. Cheers!
Upvotes: 2
Views: 1012
Reputation: 333
Try something like this? Takes the minutes in a week. multiplies it by 60 to turn it into seconds.
var d = new Date();
var minutes = 10080;
d.setTime(d.getTime() + (minutes * 60));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
Upvotes: 1