Jump to content

I cant figure out cookies


Recommended Posts

Hey guys, i need a little help understanding cookies. I feel the cookie tutorial on the site was rather poorly explained. i'm trying really hard to make sense of it, i'm sure it's a small detail im missing. http://www.w3schools.../js_cookies.asp it starts off with this code: function setCookie(c_name,value,exdays){var exdate=new Date();exdate.setDate(exdate.getDate() + exdays);var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());document.cookie=c_name + "=" + c_value;} Most of this is clear, but can someone tell me what a cookie value is? They explained c_name as a visitors name. exdays as days until cookie expires. But c_value was never explained. niether was the ternary operator in the whole tutorial. i understand the operator but not in this context. what is escape(value)? this part is what really confuses me: function getCookie(c_name){var i,x,y,ARRcookies=document.cookie.split(";");for (i=0;i<ARRcookies.length;i++){x=ARRcookies.substr(0,ARRcookies.indexOf("="));y=ARRcookies.substr(ARRcookies.indexOf("=")+1);x=x.replace(/^\s+|\s+$/g,"");if (x==c_name){return unescape(y);}}} what is var i,x,y,ARRcookies=document.cookie.split(";");? and what object or string exactly is being split? Im guessing its the data from the previous function etc.. but the splitting and slicing of an array is very confusing as the actual string being split was never shown, so i have no idea what is being done. then i guess those are regular expressions at the end? its just so confusing. that whole code including the parts i didnt paste, was only explained with 9-10 lines. if you might not be able to explain this, could you please direct me to any sites that explains the coding of cookies in greater detail? Thanks all

Edited by SnakesBite101
Link to comment
Share on other sites

The escape function will escape certain characters that have special meanings. The ternary operators adds an expiration date to the c_value string if one was given. The c_value string combines the value and the expiration date, if any.

what is var i,x,y,ARRcookies=document.cookie.split(";");?
Declaring multiple variables on the same line. The variables are separated by commas. Only the last one has a value.
and what object or string exactly is being split?
document.cookie, which contains a list of all cookies sent by the browser separated with semicolons.
then i guess those are regular expressions at the end?
That's sort of a trim pattern, it will remove excess space at the beginning and end. If you want to see what that code, or any other code, is doing, then add debugging statements. Use the developer console in your browser, and add console.log statements in the code to send whatever you want to look at to the console. You can see what c_value gets set to when you pass certain arguments to the function, you can see what the string that gets set in document.cookie is, and in the other function you can see what is being split, what x and y are set to, the values before and after the regular expression, etc.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...