Rank newbie question (forgive me)--if there's a better forum for this, let me know. I want to set a cookie that expires after thirty days. I've found a response here on stackoverflow and looked at several online explanations on how to set a cookie, but I'm having trouble fully understanding what I'm seeing. Here's the answer given on stackoverflow:
function createCookie(name, value, days) {
var date, expires;
if (days) {
date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = "; expires="+date.toGMTString();
} else {
expires = "";
}
document.cookie = name+"="+value+expires+"; path=/";
}
Here's where you get to correct me when I'm wrong (please):
Line 1 creates a function called createCookie with three arguments, name, value, and days. Those arguments are defined or set when the function is invoked with something like createCookie(testCookie, ?, 30);. I have a question mark in for the argument value because I'm not sure what goes there.
Line 2 establishes two variables, date and expires, without assigning anything to those variables.
Line 3 says if the argument days and then goes right into executing some lines. This is outside my JS understanding, as so far I've done things like if (x > 10) { where x > 10 defines a condition that when true, the code in the following brackets gets executed, and if not, it skips to the else part of the if/else. days isn't a condition as I understand it. I suppose my understanding this part isn't paramount, as long as it works.
Line 4 creates a variable called date and assigns the current date to it.
Line 5 date.setTime(date.getTime()+(days*24*60*60*1000)); does math using the function's argument days to come up with the actual expiration date and assign it to the variable date.
Line 6 expires = "; expires="+date.toGMTString(); assigns a value to the variable expires, but I don't quite follow, as it appears that value is a concatenation of expires which is undefined at that point, plus the value of the argument date expressed as a string? Also, it looks like toGMTString() is deprecated now? Again, it's probably the case that my understanding of this isn't of utmost importance; my lack thereof, though, adds to my overall confusion.
Line 8 sets the cookie with name plus the string "=" plus value plus expires plus the string "; path=/". I still don't know what value should be...
Anyone want to help me understand this, please? Thanks!