If you want your cookie to contain more information, you can set the cookie value very long. Suppose we want to save someone's name, age, and phone number:
var the_cookie = "username:thau/age:older than the hills/phone:411";
="my_happy_cookie=" + escape(the_cookie);
I use slashes/ to divide attribute names and use semicolons to distinguish different attribute names and attribute values. Slashes/and semicolons are not absolute choices, you can use any character to make the split logo, such as:
var the_cookie = "username=thau&age=older than the hills&phone=411";
="my_happy_cookie=" + escape(the_cookie);
You can choose the limiter yourself. As long as you pay attention to using the same limiter when decoding cookies.
The method when setting complex cookies is more complicated. We recommend that you use relevant arrays to save all information, assuming we save the cookie to someone's hard drive:
my_happy_cookie=username:thau/age:older than the hills/phone:411
You can put this information into a convenient related array:
function readTheCookie(the_info)
{
// load the cookie into a variable and unescape it
var the_cookie = ;
the_cookie = unescape(the_cookie);
// separate the values from the cookie name
var broken_cookie = the_cookie.split("=");
var the_values = broken_cookie[1];
// break each name:value pair into an array
var separated_values = the_values.split("/");
// loop through the list of name:values and load
// up the associate array
var property_value = "";
for (loop=0; loop<separated_values.length; loop++) {
property_value = separated_values[loop];
var broken_info = property_value.split(":");
var the_property = broken_info[0];
var the_value = broken_info[1];
the_info[the_property] = the_value;
}
}
If you have the above code in your JavaScript, you can call it like this:
var cookie_information = new Array();
readTheCookie(cookie_information);
Then you will set cookie_information["username"], cookie_information["age"], and cookie_information["phone"] correctly.
These may seem a bit difficult to understand, but in fact they are not very difficult. Let's analyze step by step:
var the_cookie = ;
Assign cookies to a variable.
var the_cookie = unescape(the_cookie);
Cancel the encoding of escape()
var broken_cookie = the_cookie.split("=");
var the_values = broken_cookie[1];
Make the_values equivalent to username:thau/age:older than the hills/phone:411.
var separated_values = the_values.split("/");
Generate an array containing 3 elements named separate_values:
separated_values[0] = "username:thau"
separated_values[1] = "age:older than the hills"
separated_values[2] = "phone:411"
for (loop=0; loop<separated_values.length; loop++) {
property_value = separated_values[loop];
var broken_info = property_value.split(":");
var the_property = broken_info[0];
var the_value = broken_info[1];
the_info[the_property] = the_value;
}
Loop calls to three elements of separate_values.
property_value = separated_values[loop];
Extract the current name:value pairing, and the first pairing is username:thau.
var broken_info = property_value.split(":");
Divide this pair into two elements in an array named broken_info:
broken_info[0] = "username"
broken_info[1] = "thau"
var the_property = broken_info[0];
The first time I passed this loop, the_property is "username"
var the_value = broken_info[1];
Its value is "thau"
the_info[the_property] = the_value;
Here we start sending back the convenient function of related arrays. It makes the_info["username"] = "thau", so now when you need to look up username from cookies you just need:
var the_name = the_info["username"];
Every time you pass this loop, add a new element to the_info. Loop to the last time,
the_info["username"] = "thau",
the_info["age"] = "old as the hills" ,
And the_info["phone"] = 411.
It's a bit tedious, but this is a good idea when you need to output a lot of information from cookies. Of course there are other ways. That is to use multiple cookies.
The method of saving multiple cookies is very intuitive. Each cookie has a name. The name of the cookie in the previous example is my_happy_cookie, we can do this:
var the_cookie ="my_happy_cookie=happiness_and_joy";
=the_cookie;
To save multiple cookies, just give each cookie a different name. If you want to add a new cookie, the cookies that have been set before will not be deleted when setting it, so:
var the_cookie ="my_happy_cookie=happiness_and_joy";
= the_cookie;
var another_cookie= "my_other_cookie=more_joy_more_happiness";
= another_cookie;
Now you need to access these two cookies, which is a bit complicated, so you need to understand the whole process. Suppose you have executed the above code and now want to access my_happy_cookie. If you view the content, you will see:
my_happy_cookie=happiness_and_joy;
my_other_cookie=more_joy_more_happiness;
This is very intuitive, but it is a bit difficult if you want to access a specific cookie. The following code can help you find a specific cookie:
function WM_readCookie(name)
{
//If there is no cookie, return false or get the value and return the value
if( == '')
return false;
else
return unescape(WM_getCookieValue(name));
}
function WM_getCookieValue(name)
{
// Declare variables.
var firstChar,lastChar;
// Get the entire cookie string.
// (This may have other name=value pairs in it.)
var theBigCookie = ;
// Grab just this cookie from theBigCookie string.
// Find the start of 'name'.
firstChar = (name);
// If you found it,
if(firstChar != -1)
{
// skip 'name' and '='.
firstChar += + 1;
// Find the end of the value string (. the next';').
lastChar = (';', firstChar);
if(lastChar == -1) lastChar = ;
// Return the value.
return (firstChar, lastChar);
}
else
{
// If there was no cookie, return false.
return false;
}
}
Below we will learn the really cool content that cookies can do.
Now you have learned how to set and read basic cookies. However, basic cookies are often automatically deleted when the user closes his browser. Sometimes this is best because the usual domain only allows 20 cookies to be retained on the user's machine. But if you want to save cookies on the user's machine, you need to set a time when the cookie is invalid, and its format is a special format called GMT. For example:
Mon, 27-Apr-1998 00:00:00 GMT
It is not easy to set up GMT correctly. You need to calculate the day of the week. Fortunately, Javascript has a date method called toGMTString that can help you. Here is an example of setting a certain time in the future:
var the_date = new Date("December 31, 2023");
var the_cookie_date =the_date.toGMTString();
Once you set the expiration date of your cookie, you must add this message before the cookie settings. Therefore your cookies should be as follows:
cookie_name=blah_blah;expires=date
Usually you just need to add expires=date to the cookie string and then use a semicolon to split different cookies.
Here is a function that creates a validity period until the end of Mayan calendar:
function setCookie()
{
// get the information
var the_name = prompt("What's your name?","");
var the_date = new Date("December 31, 2023");
var the_cookie_date =the_date.toGMTString();
// build and save the cookie
var the_cookie = "my_cookie=" + escape(the_name);
the_cookie = the_cookie +";expires=" + the_cookie_date;
= the_cookie;
}
The final cookie should look like this:
my_cookie=thau;expires=Fri,31-Dec-2023 00:00:00 GMT
After setting the cookie, it will exist in the user's machine until the expiration period. If you set the expiration date of a cookie earlier than the current time, the cookie cannot actually survive on the user's machine.
In addition, there are two things to note: path (path) and domain (domain).
This is the last obstacle to mastering cookies: by default, cookies can only be read by web pages with the cookie set in the same path on the same web server. For example, if there is a Javascript in "/food/bananas/banana_puree.htm" that asks the user for the name, you may need to access a given name in another page of your web page, such as the home page. So you have to set the path to the cookie. The path "path" is used to set the top-level directory where a cookie can be read. Setting the cookie path to the top-level directory of your web page allows all web pages under that directory to access the cookie.
Method: Add path=/ to your cookie; If you only want the web pages in the "food" directory to use this cookie, then you add path=/food;. Another point: Some websites have many small domain names, such as the net monkey may still have web pages under the domain name "," ", " and "." By default, only the web pages under the "" domain can read this cookie. If you can read the cookie to all machines under "", we must add "domain=" to the cookie.
To set a cookie at "/food/bananas/banana_puree.htm" and make it available to all web pages of monkeys, we can do this:
function setCookie()
{
var the_name = prompt("What's your name?","");
var the_cookie = "cookie_puss=" + escape(the_name) + ";" ;
var the_cookie = the_cookie + "path=/;";
var the_cookie = the_cookie + "domain=;";
= the_cookie;
}
Now we have finished learning the content of cookies. I hope you can practice more.