Ok, let's go through the readcookie function, line by line.
Code:
var pos = document.cookie.indexOf("namecookie=");
In this context, document.cookie gives us a string containing all cookies associated with the current document. We want to find the cookie whose name is namecookie in that string. If a cookie by that name exists, it will appear somewhere in that string. It will start with namecookie=, followed by the cookie's value. To find where the cookie appears in the string, we just use indexOf() to search for namecookie=
Code:
if (pos == -1) {alert("cookie not found"); return false;}
If the string namecookie= was not found, there is no cookie whose name is namecookie. Alert th use that the cookie was not found and return false (returning true or false is not necessary for this particular script to work).
Code:
var start = pos + 11;
Now we know that there is a cookie whose name is namecookie, and it starts at position pos in the string containing all cookies. However, that position is where we find "namecookie=", not the cookies value. The cookie's value will start immediately after the = sign. To get that position, just add 11 to pos (the length of the string "namecookie=").
Code:
var end = document.cookie.indexOf(";", start);
The cookie's value will be terminated with a semi colon. We want to know where the cookie's value ends, so start at position start (where the cookies value starts), and look through the string containing all cookies until we find the next semicolon. That tells us where the cookie's value ends.
Code:
if (end == -1) end = document.cookie.length;
If we do not find a semicolon, the cookie's value must extend to the end of the string containing all cookies, so we make the end equal to the end of that string.
Code:
document.getElementById("yourname").value = document.cookie.substring(start, end);
Now we now that the cookie's value starts at start and ends at end, so document.cookie.substring(start, end) will give us the cookies value. Fill that value into the form.
return true to indicate success. (again, not necessary).
Bookmarks