This example describes the use of cookie objects in JavaScript. Share to everyone for your reference. Specifically as follows:
Property
Name the only property that must be set to represent the cookie
expires specifies the lifetime of the cookie, if not set, the browser turns off automatic expiration
Path determines the availability of cookies to the server for other Web pages, in general, cookies are available to 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 subpath
Domain Many servers are composed of more than one server, domain property is mainly set in the same domain multiple servers share a cookie, if Web server a needs to share cookies with Web Server B, you need to set A's cookie's domain property to B, so A cookies created can be shared by a, b
Secure General SSL-enabled Web sites begin with HTTPS, and the secure attribute sets cookies to be accessible only through HTTPS or other security protocols
Cookies are essentially strings
In general, cookies cannot contain special characters such as semicolons, commas, spaces, and so on, but these characters can be transmitted using encoding, that is, converting private characters in a text string to the corresponding hexadecimal ASCII value, using the encodeURI () function to translate text characters into valid URIs, using the decodeURI () function to decode
Write Cookie
var cookietest = "Name=username";
Document.cookie= CookieTest; Deposit
///semicolon split different attribute
var date = Newdate ();
Date.setdate (Date.getdate () +7); Set the cookie's survival time to 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 ();
Date.settime (Date.gettime () -10000);
Document.cookie= "name=user;expires=" +date.togmtstring;
To delete a cookie is to set its expiration time to a time value of the past
I hope this article will help you with your JavaScript programming.