javascript - What is the purpose of while and for loops, if statement in this code? -


i'm trying understand concept of cookies in javascript. that, i'm trying understand working of code http://www.w3schools.com/js/tryit.asp?filename=tryjs_cookie_username:

<script> function setcookie(cname,cvalue,exdays) {     var d = new date();     d.settime(d.gettime() + (exdays*24*60*60*1000));     var expires = "expires=" + d.togmtstring();     document.cookie = cname+"="+cvalue+"; "+expires; }  function getcookie(cname) {     var name = cname + "=";     var ca = document.cookie.split(';');     for(var i=0; i<ca.length; i++) {         var c = ca[i];         while (c.charat(0)==' ') c = c.substring(1);         if (c.indexof(name) == 0) {             return c.substring(name.length, c.length);         }     }     return ""; }  function checkcookie() {     var user=getcookie("username");     if (user != "") {         alert("welcome again " + user);     } else {        user = prompt("please enter name:","");        if (user != "" && user != null) {            setcookie("username", user, 30);        }     } } </script> 

i've understood of things in code, still have doubts/queries (in getcookie function):

doubt 1. purpose of for loop

doubt 2. purpose of if

doubt 3. purpose of while loop

doubt 4. purpose of return "";

i'm asking these because script running fine without using these conditions. (i know working of each inbuilt function in getcookie function. don't understand use of above loops , conditions).

here's want say:

    function getcookie(cname) {     var name = cname + "=";     var ca = document.cookie.split(';');     var c = ca[0];     return c.substring(name.length, c.length); } 

even after i've changed expiry date past date in setcookie function (exdays*2000), still displaying "welcome xyz" message. means cookie not deleted. when run same code in different browser, deleted. why so?

doubt 1. purpose of loop

all cookies saved semicolon separated key-value (actually semicolon , space ;). key , values separated equal-to (=) symbol. iterating, checking keys , returning value of key matching argument passed method

doubt 2. purpose of if

to check if current key iteration contains cookie name passed method.

doubt 3. purpose of while loop

to remove spaces 1 one. 1 can improved using trim() method.

//while (c.charat(0)==' ') c = c.substring(1); c = c.trim(); 

doubt 4. purpose of return "";

if there no value found, return empty string rather user having check null or undefined.

even after i've changed expiry date past date in setcookie function (exdays*2000), still displaying "welcome xyz" message. means cookie not deleted. when run same code in different browser, deleted. why so?

it possible (assuming this, since have not shared fiddle) setcookie method not executed when opened page second time on same browser.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -