1. What is Cookie?
Cookies are used to save information on pages, such as automatic logon and user name remembering.
Ii. Cookie features
- All pages on the same website share a set of cookies.
- Cookie quantity and size limit
- Cookie expiration time
3. How to use cookies?
Use document. Cookie to write cookies
<! Doctype html > < Html Lang = "En-us" > < Head > < Meta Charset = "UTF-8" > < Title > Cookie Basics </ Title > </ Head > < Body > </ Body > </ Html > < Script Type = "Text/JavaScript" > Document. Cookie = ' Username = ABC ' ; Document. Cookie = ' Password = 123 ' ; Document. Cookie = ' Email = abcdef@123.com ' ; </ Script >
Open a browser to view the cookie. You can find that the new Cookie does not overwrite the original one.
If no expiration time is set, the cookie will be cleared when the browser is closed. How do I set the expiration time? The answer is: expires. We generally use the date object in combination.
VaROdate =NewDate (); odate. setdate (odate. getdate ()+ 30); Document. Cookie= 'Username = ABC; expires = '+ odate;
We can see from the Firefox browser that the expiration time of username is 30 days after the current time.
Finally, encapsulate the method for obtaining the cookie:
FunctionSetcookie (name, value, iday ){VaROdate =NewDate (); odate. setdate (odate. getdate ()+Iday); document. Cookie= Name + '=' + value + '; expires =' +Odate ;}
I learned how to set a cookie. How can I read the cookie?
First, let's take a look at what type of cookie content is?
Document. Cookie = 'username = abc'; Document. Cookie= 'Password = 66661'; Document. Cookie= 'Email = abcdef@123.com';TypeofDocument. Cookie;//StringAlert (document. Cookie );//'Username = ABC; Password = 123; email = abcdef@123.com'
The result is a string. Note that each; is followed by a space.
So how can we get the specific value? AttachedCode:
FunctionGetcookie (name ){VaRArr = Document. Cookie. Split (';');For(VaRI = 0; I <arr. length; I ++){VaRArr2 = arr [I]. Split ('=');If(Arr2 [0] =Name ){ReturnArr2 [1] ;}}Return'';}
In addition to setting and obtaining cookies, we can also delete them. We often see the function of clearing user names on the Internet, which is actually used to clear cookies.
Clearing a cookie is actually very easy. You only need to set the expiration time to yesterday. Use the previously encapsulated setcookie to set the cookie.
FunctionRemovecookie (name) {setcookie (name,'1',-1);}
Finally, we encapsulate the setting, obtaining, and clearing cookies into a cookie. js
// Set cookie Function Setcookie (name, value, iday ){ VaR Odate = New Date (); odate. setdate (odate. getdate () + Iday); document. Cookie = Name + '=' + value + '; expires =' + Odate ;} // Read cookie Function Getcookie (name ){ VaR Arr = Document. Cookie. Split (';' ); For ( VaR I = 0; I <arr. length; I ++ ){ VaR Arr2 = arr [I]. Split ('=' ); If (Arr2 [0] = Name ){ Return Arr2 [1] ;}} Return '' ;} // Clear cookie Function Removecookie (name) {setcookie (name, '1',-1 );}
PS: attach a small instance of the cookie application.Click here