JavaScript Cookie Object

Source: Internet
Author: User


Name the only property that must be set, which represents the name of the cookie

expires Specifies the lifetime of the cookie, if not set, the browser turns off auto-fail

path Determines whether the cookie is available to the server for other Web pages, in general, the cookie is available for all pages in the same directory, and when the Path property is set, the cookie is only valid for all pages under the specified path and sub-path

domain Many servers consist of multiple servers, the domain property primarily sets multiple servers under the same domain to share a cookie, and if Web server a needs to share cookies with Web Server B, the domain of A's cookie needs to be property is set to B so that a cookie created by a can be shared by A and B

Secure General SSL-enabled Web sites start with HTTPS, and the secure property can set cookies to be accessed only through HTTPS or other security protocols

A cookie is essentially a string

In general, cookies cannot contain special characters such as semicolons, commas, and spaces, but they can be transmitted using encoding, which translates the private characters in the text string into corresponding hexadecimal ASCII values, using encodeURI () function converts a literal character to a valid URI using the decodeuri () function to decode

Write Cookie

var cookietest = "Name=username";d ocument.cookie= cookietest;  Deposit//Semicolon split different attributes var date = Newdate ();d ate.setdate (Date.getdate () +7);  Set the cookie to survive for one week document.cookie= encodeURI ("Name=user") + "; expires=" +date.toutcstring ();

Read cookies

var cookiestring= decodeuri (Document.cookie), var cookiearray= cookiestring.split (";"); for (vari=0;i< cookiearray.length;i++) {      var cookienum = cookiearray[i].split ("=");      var cookiename = cookienum[0];      var cookievalue = cookienum[1];}

Delete Cookies

var date = Newdate ();d ate.settime (Date.gettime () -10000);d ocument.cookie= "name=user;expires=" +date.togmtstring;// Deleting a cookie is setting its expiration time to a time value of the past

JavaScript Cookie Object

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: 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.