JS in cookies, Sessionstorage, localstorage

Source: Internet
Author: User

A. Cookies

1 <!DOCTYPE HTML>2 <HTML>3     <Head>4         <MetaCharSet= "Utf-8">5         <title></title>6     </Head>7     <Body>8         <inputtype= "text"name= "username"ID= "username"value="">9         <inputtype= "Password"name= "Password"ID= "Password"value="">Ten         <Buttontype= "button"name= "button"ID= "BTN">Delete Cookies</Button> One         <Scripttype= "Text/javascript"> A             //1. Set up, get -             varD= NewDate (); - d.sethours (d.gethours ()+ 1);//expires after 1 hours the             //path=/This cookie can be accessed by all directories in the site at this time/on behalf of - Document.cookie='username=zhangsan;expires='+d.toutcstring ()+";p ath=/";//Set Cookies - Document.cookie;//get all the cookies - Console.log (Document.cookie) +  -             //2. Setting, acquiring, and deleting cookies +             //setcookie (' Password ', ' 123456 ', 0.5*3600*1000)//30 minutes after expiration A             //Setcookie (' username ', ' Zhangsan ', 0.5*3600*1000)//expires in 30 minutes at             varBtnobj=document.getElementById ('btn') - Btnobj.onclick= function(){ - Delcookie ('username'); - Delcookie ('Password'); -             } -             functionSetcookie (name,value,expires) { in                 //Calculate Validity period -                 varD= NewDate (); to                 //Set Validity period + D.settime (D.gettime ()+expires); -                 //Set Cookies the Document.cookie=name+'='+value+'; expires='+d.toutcstring (); *             } $             functionGetCookie (name) {Panax Notoginseng                 //Get Cookies -                 varcookiestr=Document.cookie; the                 //to divide or divide +                 varCookiearr=Cookiestr.split (';'); A                 //get the corresponding cookie value the name+= '=';//The cookie value obtained is followed by = +                 varresult= "'; -                  for(varI=0; I<Cookiearr.length;i++) { $                     //not a cookie is worth a space before, remove the space, and then find the corresponding name value $                     vartmp=Cookiearr[i].trim (); -                     if(Tmp.indexof (name)== 0) { -                         //If the lookup succeeds, return directly to the the result=Tmp.slice (name.length); -                          Break;Wuyi                     } the                 } -                 //return Results Wu                 returnresult; -             } About             functionDelcookie (name) { $                 varD= NewDate (); - D.settime (D.gettime ()- +); - Document.cookie=name+'=;expires='+d.toutcstring (); -             } A         </Script> +     </Body> the </HTML>

JS in cookies, Sessionstorage, localstorage

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.