0

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!

Community
  • 1
  • 1
  • The value is "What you want to store in the cookie" and you need to learn about truthy values. – epascarello Jun 01 '15 at 19:19
  • For `Line 3`, everything in JavaScript can be used as a condition because every value has either a "truthy" or "falsey" evaluation. The "falsey" values are `0`, `NaN`, `""`, `null`, `undefined` and `false`. –  Jun 01 '15 at 19:22
  • Thanks for the help guys--appreciate it! – Matt Clara Jun 02 '15 at 12:04

2 Answers2

0

Line 1 - value is the data you want to store in the cookie.

createCookie("testCookie", "The value I want to store", 30);

Line 3 - That is a truthy check, it is checking to make sure days is not undefined.

Line 6 - I think you are confusing the string "expires" for the variable expires. And for the GMT, probably written before it was deprecated.

epascarello
  • 204,599
  • 20
  • 195
  • 236
0
function createCookie(name, value, days) { // this passes in the values for the cookie you want to store
    var date, expires; // set the variables
    if (days) { // check to make sure days is not undefined
        date = new Date(); // sets todays date
        date.setTime(date.getTime()+(days*24*60*60*1000)); // does maths to make it expiry after `day` parameter you passed into the function.
        expires = "; expires="+date.toGMTString(); // sets the expiry date
    } else {
        expires = ""; // else if days is undefined sets nothing to expiry
    }
    document.cookie = name+"="+value+expires+"; path=/"; // sets the cookie.
}
Josh Stevens
  • 3,943
  • 1
  • 15
  • 22