Using Cookies we already know that there is a cookie attribute in the document Object. But what is Cookie? "Some websites store some information on your hard disk with small text files, which are called cookies ." -- MSIE help. In general, Cookies are created by CGI or similar files and programs that are more advanced than HTML,
Using Cookies we already know that there is a cookie attribute in the document Object. But what is Cookie? "Some websites store some information on your hard disk with small text files, which are called cookies ." -- MSIE help. In general, Cookies are created by CGI or similar files and programs that are more advanced than HTML, but javascript also provides comprehensive access rights to Cookies.
Before continuing, we should first learn the basic knowledge of cookies.
Each Cookie is like this: = <值>
The restriction is similar to the naming restriction of javascript, with the addition of "cannot use javascript keywords" and "can only be used in URL encoding ". The latter is hard to understand, but as long as you only use letters and numbers for naming, there is no problem at all. <值> The requirement is "only characters that can be used in URL encoding ".
Each Cookie has an expiration date. Once the computer clock expires, the Cookie will be deleted. You cannot delete a Cookie directly, but you can delete it indirectly by setting the expiration date earlier than the current time.
Each webpage, or site, has its own Cookies. These Cookies can only be accessed by webpages under the site, from webpages in other sites or in unauthorized areas under the same site, is inaccessible. Each "group" Cookie has a specified total size (about 2 kb per "group"). If the total size exceeds the maximum size, the first invalid Cookie is deleted first, to make the new Cookie "home ".
Now let's learn how to use the documents. cookie attribute.
If you directly use documents. cookie attribute, or use some method, such as assigning values to variables to obtain documents. cookie value, we can know the number of Cookies in the current document, the name of each cookie, and its value. For example, add "document. write (documents. cookie)" to a document. The result is as follows:
Name=kevin;email=kevin@kevin.com?lastvisited=index.html
This means that the document contains three Cookies: name,emailand lastvisited. their values are kevin,kevin@kevin.comand index.html. We can see that two Cookies are separated by semicolons and spaces, so we can use cookieString. split (';') method to obtain an array separated by each Cookie (use varcookieString = documents first. cookie ).
The method for setting a Cookie is to assign values to documents. cookie. Unlike the assignment in other cases, assigning values to the Cookies. cookie does not delete the original Cookies, but only adds or modifies the original Cookies. Assignment format:
Events. cookie = 'cookiename = '+ escape ('cookievalue ')
+ '; Expires =' + expirationDateObj. toGMTString ();
Are you dizzy? The above is not the place where the bold text is to be copied without mistake, the bold text is to be changed according to the actual situation. CookieName indicates the Cookie name, cookieValue indicates the Cookie value, and expirationDateObj indicates the date object name storing the expiration date. If you do not need to specify the expiration date, the second row is not required. If no expiration date is specified, the browser expires after the browser is closed (that is, all windows are closed) by default.
Have you seen the underline above? These are important points.
First, the escape () method: Why must it be used? Because the Cookie value must be "only characters that can be used in URL encoding ". We know that the "escape ()" method is to encode the string by URL encoding, then we only need to use an "escape ()" method to process the value output to the Cookie, you can use "scape SCAPE ()" to process the values received from cookies. The most common use of these two methods is to process Cookies. In fact, setting a Cookie is only "events. cookie = 'cookiename = cookieValue '"is so simple, but it is better to use an escape () to avoid any characters in the URL in cookieValue.
Then the semicolon before "expires" is noted. It is a semicolon rather than a semicolon.
Finally, toGMTString () method: Set the Cookie's validity period date to use the GMT format, and other formats of time do not work.
Now let's take a look. Set a Cookie named "name = rose" and expire three months later.
Varexpires = newDate ();
Expires. setTime (expires. getTime () + 3*30*24*60*60*1000 );
/* Three months x one month treated as 30 days x 24 hours a day
X hour 60 Minutes x minute 60 seconds x second 1000 ms */
Documents. cookie = 'name = rose; expires = '+ expires. toGMTString ();
Why not use the escape () method? This is because we know that rose is a legal URL encoded string, that is, 'Rose '= escape ('Rose '). Generally, if you do not use escape () when setting a Cookie, you do not need to use unescape () when obtaining the Cookie ().
Next time: Write a function to find the value of the specified Cookie.
FunctiongetCookie (cookieName ){
VarcookieString = documents. cookie;
Varstart = cookieString. indexOf (cookieName + '= ');
// The reason for adding the equal sign is to avoid having
// The same string as cookieName.
If (start =-1) // cannot be found
Returnnull;
Start + = cookieName. length + 1;
Varend = cookieString. indexOf (';', start );
If (end =-1) returnunescape (cookieString. substring (start ));
Returnunescape (cookieString. substring (start, end ));
}
This function uses some methods of the string object. If you do not remember (Are you so unremembered), please check it out. All the if Statements of this function do not contain the else. This is because if the condition is true, the program runs the return statement, and the operation stops when the return statement is run in the function, so it's okay if you don't add else. When this function finds the Cookie, it will return the Cookie value; otherwise, "null" is returned ".
Now we want to delete the set name = roseCookie.
Varexpires = newDate ();
Expires. setTime (expires. getTime ()-1 );
Documents. cookie = 'name = rose; expires = '+ expires. toGMTString ();
As you can see, you only need to change the expiration date to a little earlier than the current date (this is 1 millisecond earlier), and then set the Cookie in the same way to delete the Cookie.