Jump to content

Am I missing something or am I not cut out for this?


BooKwiznak

Recommended Posts

Hello W3School forum,I'm new to the javascript language and been following the tutorials on this site. It's been very helpful and made me understand the "concept" of javascripting...I think. I came upon a roadblock when I got to the "cookies" section. I can go through the example code, but I'm lost in what's going on or basically understanding it. I kept going back to the tutorials but I got lost and finally I asked myself, "How am I going to write all this from scratch or what do I have to remember?" So I'm a bit depressed, because I'm trying hard to understand everything and memorize everything much as I can, but it doesn't seem to be helping me as much as I thought it would. I really want to get javascript down, but I wanted to ask anyone here who considers themselves either "intermediate" or a "master" of this language, "What am I doing wrong?" I've been trying to get my spirits up but I'm beating myself up instead. I'm going to try again and again. I even went as far as to subscribe to Lynda.com, but the javascript tutorial on there seems outdated and I'm learning the concept through making a bingo card. It's also teaching me everything from backwards in a sense. Sigh.

Link to comment
Share on other sites

The getCookie and setCookie functions in the tutorial can be copied and pasted into your script with no changes. If you are not sure how to use them, please ask a more specific question. If you want to UNDERSTAND them, please post a question about 1-2 specific things you find confusing. We can drag out the conversation until you understand all of it, if you like.

Link to comment
Share on other sites

I think you're digging yourself a grave by trying to memorize everything. Don't memorize the content. Learn where to look when you need to know something. Even "masters" don't have everything memorized. They still need to use references on occasion. Anything they do have memorized is simply a result of frequent use. The trick is to know what you are looking for and where to look for it.

Link to comment
Share on other sites

The getCookie and setCookie functions in the tutorial can be copied and pasted into your script with no changes. If you are not sure how to use them, please ask a more specific question. If you want to UNDERSTAND them, please post a question about 1-2 specific things you find confusing. We can drag out the conversation until you understand all of it, if you like.
Thanks for taking the task to help me out! I really appreciate it.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;}I guess I'll start with the first block in the example. I pretty much lose it after declaring the var exdate. Where does the '+exdays' come from? What does it mean? The same with escape(value). p.s. I understand that in time I'll remember bits and pieces of code, but is there something that you as an experienced user would tell anyone to "put in stone" what I need to remember or keep in mind at all times when using or learning javascript?
Link to comment
Share on other sites

I think you're digging yourself a grave by trying to memorize everything. Don't memorize the content. Learn where to look when you need to know something. Even "masters" don't have everything memorized. They still need to use references on occasion. Anything they do have memorized is simply a result of frequent use. The trick is to know what you are looking for and where to look for it.
Thanks! I'll keep that in mind!
Link to comment
Share on other sites

why don't you show us what you have and what you are having problems with?
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); } }}Is ARRcookies a built-in term? Also the block of code inside the for loop is insane! Especially the x=x.replace(/^\s+|\s+$/g,""); part!
Link to comment
Share on other sites

Is ARRcookies a built-in term?
No it's declared in the first line with the rest of the variables:var i,x,y,ARRcookies=document.cookie.split(";");document.cookie just retrieves the cookie. The split function creates an array splitting the string any time there's a semicolon.
Also the block of code inside the for loop is insane! Especially the x=x.replace(/^\s+|\s+$/g,""); part!
Heh. :) That's a regular expression (or regex). Those look very confusing at first, but once you understand them a little they start to get less intimidating. Basically it's just looking for a white space character at the beginning or the end of a string and replaces them with an empty string (in other words, it removes white space from the beginning and end of the string). Look here for more on regexes.
Link to comment
Share on other sites

The getCookie and setCookie functions in the tutorial can be copied and pasted into your script with no changes. If you are not sure how to use them, please ask a more specific question. If you want to UNDERSTAND them, please post a question about 1-2 specific things you find confusing. We can drag out the conversation until you understand all of it, if you like.
I understand what's going on in the function checkCookie section! It makes sense. But the other setCookie & getCookie is turning my brain into mush...argh.
Link to comment
Share on other sites

Thanks for taking the task to help me out! I really appreciate it.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;}I guess I'll start with the first block in the example. I pretty much lose it after declaring the var exdate. Where does the '+exdays' come from? What does it mean? The same with escape(value). p.s. I understand that in time I'll remember bits and pieces of code, but is there something that you as an experienced user would tell anyone to "put in stone" what I need to remember or keep in mind at all times when using or learning javascript?
Well, exdays is defined in the function's constructor, which are variables sent to a function when its called. That's this part
function setCookie(c_name,value,exdays)

this line is a doozy though for beginners, though,... probably.

var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());

First, escape is a native javascript function.http://www.w3schools.com/jsref/jsref_escape.aspeverything after the first + is a ternary statement, which is just a shorter way of writing an if else statement. This is how it would be re-written in that context.

if(exdays == null){  var c_value = escape(value) + "";}else{  var c_value = escape(value) + "; expires =" + exdate.toUTCString();};

edit: edited the ternary to if/else statement bit.

Link to comment
Share on other sites

One little error in your conversion, scientist:
if(exdays == null){  var c_value = escape(value) + "";}else{  var c_value = escape(value) + "; expires =" + exdate.toUTCString();};

got me!
Link to comment
Share on other sites

Well, exdays is defined in the function's constructor, which are variables sent to a function when its called. That's this part
function setCookie(c_name,value,exdays)

this line is a doozy though for beginners, though,... probably.

var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());

First, escape is a native javascript function.http://www.w3schools.com/jsref/jsref_escape.aspeverything after the first + is a ternary statement, which is just a shorter way of writing an if else statement. This is how it would be re-written in that context.

if(exdays == null){  var c_value = escape(value) + "";}else{  var c_value = escape(value) + "; expires =" + exdate.toUTCString();};

edit: edited the ternary to if/else statement bit.

Thanks! I'm still a bit confused, but I'm beginning to understand. I noticed everyone here is pointing me to the JS reference section. I take it that's where I should head to first before I tackle the JS Advanced section?
Link to comment
Share on other sites

No it's declared in the first line with the rest of the variables:var i,x,y,ARRcookies=document.cookie.split(";");document.cookie just retrieves the cookie. The split function creates an array splitting the string any time there's a semicolon.Heh. :) That's a regular expression (or regex). Those look very confusing at first, but once you understand them a little they start to get less intimidating. Basically it's just looking for a white space character at the beginning or the end of a string and replaces them with an empty string (in other words, it removes white space from the beginning and end of the string). Look here for more on regexes.
Ok, so I should check out the RegEx reference section. Thank you, I appreciate that your helping me out!
Link to comment
Share on other sites

Here's one item: x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));ARRcookies[i] identifies an element of the ARRcookies array, which happens to be a string. What the string looks like (for example) is "name=Bob". What we want to do is extract "name" from this string. The problem is, we don't know in advance that the word IS "name". That is the purpose of the function.Every string object has a substr method. It returns a substring (a truncated section) of the entire string. The first argument passed to substr identifies the index of the substring's first character. In this case, we want to start with the first character, whose index is 0. The second argument is the length of the substring. Since there is no way to know this value in advance, it must be computed. JavaScript allows you to pass a function call instead of data itself, as long as the return value of the function is the data type expected. substr expects both arguments to be numbers. indexOf returns a number, so that's okay. Specifically, ARRcookies[i].indexOf("=") returns the location of the first "=" character in the string. This is also the length of the data between the first character and the "=" character. So that's how we get "name". It would look a lot simpler if we expand this thing and used variable names that mean something. So the following is equivalent to the line of code I've been discussing. Note: I'm using the word "key" in this code, because cookies are defined in key-value pairs. In the string "name=Bob", "name" is the key and "Bob" is the value. All we want this line of code to do is return the key.

this_cookie = ARRcookies[i];this_cookie_key_length = this_cookie.indexOf("=");this_cookie_key = this_cookie.substr(0, this_cookie_key_length);

The next line of code uses the same technique to get the value.

Link to comment
Share on other sites

Here's one item: x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));ARRcookies[i] identifies an element of the ARRcookies array, which happens to be a string. What the string looks like (for example) is "name=Bob". What we want to do is extract "name" from this string. The problem is, we don't know in advance that the word IS "name". That is the purpose of the function.Every string object has a substr method. It returns a substring (a truncated section) of the entire string. The first argument passed to substr identifies the index of the substring's first character. In this case, we want to start with the first character, whose index is 0. The second argument is the length of the substring. Since there is no way to know this value in advance, it must be computed. JavaScript allows you to pass a function call instead of data itself, as long as the return value of the function is the data type expected. substr expects both arguments to be numbers. indexOf returns a number, so that's okay. Specifically, ARRcookies[i].indexOf("=") returns the location of the first "=" character in the string. This is also the length of the data between the first character and the "=" character. So that's how we get "name". It would look a lot simpler if we expand this thing and used variable names that mean something. So the following is equivalent to the line of code I've been discussing. Note: I'm using the word "key" in this code, because cookies are defined in key-value pairs. In the string "name=Bob", "name" is the key and "Bob" is the value. All we want this line of code to do is return the key.
this_cookie = ARRcookies[i];this_cookie_key_length = this_cookie.indexOf("=");this_cookie_key = this_cookie.substr(0, this_cookie_key_length);

The next line of code uses the same technique to get the value.

Thanks a bunch! I get certain parts of what your explaining and some of it seems overwhelming...I guess I need to go through the JS reference section more thoroughly!
Link to comment
Share on other sites

Before you dig into the reference section you might want to finish tutorials. The references don't really teach you anything. They are there to look things up when you need to.If you haven't already, you might want to go through this section. That should get you started on using some of the built in functions for the various types of objects in JavaScript (including regexes).

Link to comment
Share on other sites

Before you dig into the reference section you might want to finish tutorials. The references don't really teach you anything. They are there to look things up when you need to.If you haven't already, you might want to go through this section. That should get you started on using some of the built in functions for the various types of objects in JavaScript (including regexes).
Thanks ShadowMage, I panicked when I came upon the Cookies section. I was following all the tutorials up to then and felt like I hit a wall when I saw the code for setting and getting Cookies! I'll continue on through the tutorials and come back to the Cookies section and have another crack at it. I'm not giving up.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...