1. |
Start by initializing the variable outMsg, which will contain the message we want to display.
|
2. |
if (document.cookie == "") {
outMsg = "There are no cookies here";
This conditional test is read "If the outMsg object is null (that is, empty), then write, "There are no cookies here" into the document window.
|
3. |
var thisCookie = document.cookie. split("; ");
If the previous test failed (i.e., if there was at least one cookie present), then get the values of all of the cookies using document.cookie.split("; ") and stuff those values into a array called thisCookie. Remember that the split("; ") command creates an array of all of the cookies. Later, the script will be able to reference each of the values in that array.
|
| |
4. |
for (var i=0; i<thisCookie.length; i++) {
This line starts a loop by first setting the value of i, the counter variable, to 0. Then, if i is less than the number of cookies in the thisCookie array, increment the value of i by 1.
|
5. |
outMsg += "Cookie name is '" + thisCookie[i].split("=")[0]);
outMsg += "', and the value is '" + thisCookie[i].split("=")[1] + "'<br />");
As the script moves through the array, it puts the text string "Cookie name is '" into outMsg, followed by the name of the cookie. Then it concatenates the text string "', and the value is '" and the value of the cookie. And at the end of each line, we add an HTML break.
|
6. |
document.getElementById. ("cookieData").innerHTML = outMsg;
After setting the variable outMsg, it gets dumped out to the page via innerHTML when all the cookies have been gone through.
|