Jump to content

Cookie explain


Matej

Recommended Posts

Hi , i recently found this code

 

function getCookie(cname){var name = cname + "=";var ca = document.cookie.split(';');for(var i=0; i<ca.length; i++)   {  var c = ca[i].trim();  if (c.indexOf(name)==0) return c.substring(name.length,c.length);  }return "";} 

 

If i get it right var c is now array which consist of elements in array "ca" but with space removed (basicly c=c).

 

also can someone explain to me what is "name" in "c.indexOf(name)==0)"

where did i deffine it? will i deffine it by apling function?

 

Thanks for answering quite stupid question.

Link to comment
Share on other sites

name is based on the parameter given to the function, it is given a value when the function is called.

 

document.cookie is a string composed of key=value pairs separated by semi-colons ;

The function breaks the cookie string into pieces and gets the value from the one where the key is the same as the name that was passed to the function.

Link to comment
Share on other sites

Thanks for answers sirs , so if i get it right :

1) we have cookie name , for example "Cheese" , so basicly "Cheese=somevalue".(we have another like ;expire=date and so on)

2) we set cname to "Cheese"

3)we set cookies to new Array , (so Array[0]="Cheese=someovalue" , Array[1]="expire=date" and so on)

4)we set "var c" to loop throught Array , and delete space in front of and behind Array[0] etc. value

5)and if for example "c.indexOf(name)==0" so if "Cheese=somevalue".instanceOf(Cheese)==0 it return "Cheese=Somevalue.substring(name.length(7),c.length(2)) , also why cant it be ca.length?

 

Thanks for answers

Link to comment
Share on other sites

A cookie typically has a string which is a semi-colon delimited list of key/value pairs. in an example cookie, say that the string value is: "Cheese=somevalue;expire=date;hello=new world"document.cookie.split(';') will basically "split" this string by the semi-colons, expanding the the pairs into an array like so:var ca = ['Cheese=somevalue', 'expire=date', 'hello=new world'];The for loop then iterates through each and every element in the array and tries to find the name which you've passed into the function. With "Cheese" passed in, it looks for the first element that has "Cheese=" in it and parses out that portion of the string via substring, which will return "somevalue".Here is another way you can parse the multiple values.

var Cookie_Object = ({  values: {},  load:function () {        var arr = document.cookie.split(';');    for(var i=0,key,value,split; i<arr.length;i++){       split= arr[i].indexOf("=");       if(split==-1){           // if the "=" was missing, assume the key IS the value           key = value = arr[i].trim();       }else{           key   = arr[i].substring(0,split)).trim();           value = arr[i].substring(split+1).trim();       }       this.values[key] = value;    }    return this;  }}).load(); 

You see, this code automatically parses the current cookie into an object where all the values are immediately accessible and you don't need to rerun the function. so going by the previous cookie values we've used, you can go:Cookie_Object.values["Cheese"]and the code will return to you "somevalue".However, I should say that you should move away with using cookies. The new HTML specifications have basically migrated the use of cookies to the localstorage and sessionstorage variables. Using Web Storage is much faster, more secure, can hold far, FAR more information, and you don't have to worry about parsing things to the right datatypes and so forth, so you can preserve entire complex data structures while a cookie can only store a string.

Link to comment
Share on other sites

A collection is similar to an array. A function like getElementsByTagName will return a collection of node objects, for example.That example is adding a new method to all array objects. Inside the function, this refers to the actual array that you're using. When the myFunction function calls fruits.uCase, and the uCase method runs, this refers to the fruits array.

Link to comment
Share on other sites

Thanks , i undesrtood that example with prototype , i was (and im still quite :D) confused if i should use c=blabla.method() or c=blabla.method()

But if i get it right , when im working with arrays and collections , there has to be , if i use "this"

Link to comment
Share on other sites

confused if i should use c=blabla.method() or c=blabla.method()

It depends what c is set to. If c is an array, and you are looping through it to run a function on each element in the array, then you need to use the index. If you want to run a function on all of c, or if c is not an array, then you don't use an index.
Link to comment
Share on other sites

So

for(var i=0; i<ca.length; i++)   {  var c = ca[i].trim();

im setting C and C isnt array so i dont use index

 

and

Array.prototype.myUcase = function() {    for (i = 0; i < this.length; i++) {        this[i] = this[i].toUpperCase();    }}

this reffer to array so i need to put , did i get it right? :)

 

and if i would use for example

c=ca[i].split("=")

(in loop) i would have to use c eventually, bcs c would became array right?

 

//

 

And basicly when im using

var c = ca[i].trim();
C isnt becoming an Array

Thanks for answers :)

Edited by Matej
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...