Use of cookies in the Web

Source: Internet
Author: User

A: Where does the cookie find the write success in the browser?

  

Second: How to write with JS

  

functionAddcookie (name,value,expirehours) {varcookiestring=name+ "=" +escape (value) + "; path=/"; //determine if the expiration time is set    if(expirehours>0){        varDate=NewDate (); Date.settime (Date.gettime ()+expirehours*3600*1000); Cookiestring=cookiestring+ "; expires=" +date.togmtstring ();//expires Specifies the lifetime of the cookie, by default the cookie is present, and the value they store exists only during the browser session, and the values are lost when the user launches the browser.    If you want a cookie to exist for a period of time, set the Expires property to a future expiration date. } document.cookie=cookiestring;}
Addcookie (' username ', ' Wei ', 12)

Three: How to read and write

function GetCookie (name) {    var strcookie=document.cookie;//You can use Console.log () print in the console to    view var arrcookie=strcookie.split (";" );      for (var i=0;i<arrcookie.length;i++) {    var arr=arrcookie[i].split ("=");     if(arr[0]==name)return unescape (arr[1]);    }     return NULL ;}
Get (' Wei ')

Four: How to delete

function Delcookie (name) {// Delete cookie    varnew  Date ();     -1);     var cval=GetCookie (name);     if (cval!=null) document.cookie= name + "=" +cval+ "; path=/;expires= "+exp.togmtstring ();}
Delcookie (' Wei ')

Use of cookies in the Web

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.