Cookie objects in javascript

Source: Internet
Author: User

Cookie objects in javascript

Attribute

NameUnique attribute that must be set, indicating the cookie name

ExpiresSpecifies the cookie lifecycle. If this parameter is not set, the browser will automatically expire when it is disabled.

PathDetermine the availability of cookies for other web pages on the server. Generally, cookies are available for all pages in the same directory. After the path attribute is set, cookie is only valid for all webpages in the specified path and sub-Path

DomainMany servers are composed of multiple servers. The domain attribute mainly sets multiple servers in the same domain to share a cookie. If web server a needs to share a cookie with web Server B, set the domain attribute of cookie a to B so that the cookie created by a can be shared by a and B.

SecureGenerally, SSL-enabled websites start with HTTPS. The secure attribute can be set to cookies that can only be accessed through HTTPS or other security protocols.

Cookie is essentially a string

Generally, Cookies cannot contain special characters such as semicolons, commas, and spaces. However, these characters can be transmitted using encoding, that is, convert the special characters in the text string to the corresponding hexadecimal ASCII value. You can useEncodeURI ()The function converts text characters to valid Uris.DecodeURI ()Function Decoding

Write cookie

Var cookieTest = "name = userName"; document. cookie = cookieTest; // save // use a semicolon to separate different attributes var date = newDate (); date. setDate (date. getDate () + 7); // set the cookie survival time to a week document. cookie = encodeURI ("name = user") + "; expires =" + date. toUTCString ();

Read cookie

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 cookie

Var date = newDate (); date. setTime (date. getTime ()-10000); document. cookie = "name = User; expires =" + date. toGMTString; // when a cookie is deleted, the expiration time is set to a previous time value.


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.