Matej Posted May 14, 2014 Share Posted May 14, 2014 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 More sharing options...
Ingolme Posted May 14, 2014 Share Posted May 14, 2014 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 More sharing options...
davej Posted May 14, 2014 Share Posted May 14, 2014 The topic comes up periodically... http://w3schools.invisionzone.com/index.php?showtopic=49961 ...and the tutorial is here... http://www.w3schools.com/js/js_cookies.asp Link to comment Share on other sites More sharing options...
Matej Posted May 14, 2014 Author Share Posted May 14, 2014 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 More sharing options...
Hadien Posted May 14, 2014 Share Posted May 14, 2014 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 More sharing options...
Matej Posted May 14, 2014 Author Share Posted May 14, 2014 (edited) Thanks for advice Edited May 14, 2014 by Matej Link to comment Share on other sites More sharing options...
Matej Posted May 15, 2014 Author Share Posted May 15, 2014 One more question , why is there var c = ca[i].trim();? and here this[i]=this[i].toUpperCase() isthere " this" and not just "this"?(Its the example from w3schools) Link to comment Share on other sites More sharing options...
justsomeguy Posted May 15, 2014 Share Posted May 15, 2014 In the context where they use that, this must refer to something like an array or collection. Link to comment Share on other sites More sharing options...
Matej Posted May 15, 2014 Author Share Posted May 15, 2014 So there always have to be , when i wanan use this=bla bla ? Link to comment Share on other sites More sharing options...
justsomeguy Posted May 15, 2014 Share Posted May 15, 2014 No, there won't. It depends on the context of the code. You use that syntax when you are working with arrays or collections. Link to comment Share on other sites More sharing options...
Matej Posted May 15, 2014 Author Share Posted May 15, 2014 What do you mean as collections? Objects? Link to comment Share on other sites More sharing options...
davej Posted May 15, 2014 Share Posted May 15, 2014 Where is the exact code you are talking about? Which example? Link to comment Share on other sites More sharing options...
Matej Posted May 15, 2014 Author Share Posted May 15, 2014 http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_prototype_array Link to comment Share on other sites More sharing options...
justsomeguy Posted May 15, 2014 Share Posted May 15, 2014 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 More sharing options...
Matej Posted May 16, 2014 Author Share Posted May 16, 2014 Thanks , i undesrtood that example with prototype , i was (and im still quite ) 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 More sharing options...
justsomeguy Posted May 16, 2014 Share Posted May 16, 2014 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 More sharing options...
Matej Posted May 16, 2014 Author Share Posted May 16, 2014 (edited) 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 ArrayThanks for answers Edited May 16, 2014 by Matej Link to comment Share on other sites More sharing options...
justsomeguy Posted May 16, 2014 Share Posted May 16, 2014 Right. Arrays do not have a trim method, strings have a trim method. The trim method returns a string, so c is a string. Link to comment Share on other sites More sharing options...
Matej Posted May 17, 2014 Author Share Posted May 17, 2014 Thanks! and last question if i set var g=x.children g becomes array right? Link to comment Share on other sites More sharing options...
davej Posted May 17, 2014 Share Posted May 17, 2014 What does this topic have to do with cookies? childNodes: https://developer.mozilla.org/en-US/docs/Web/API/Node.childNodes children: https://developer.mozilla.org/en-US/docs/Web/API/ParentNode.children Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now