[JavaScript] cookie Basics

Source: Internet
Author: User
Tags set cookie
1. What is Cookie?

Cookies are used to save information on pages, such as automatic logon and user name remembering.

Ii. Cookie features
    1. All pages on the same website share a set of cookies.
    2. Cookie quantity and size limit
    3. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.