JavaScript data storage Cookie, javascriptcookie

Source: Internet
Author: User
Tags send cookies set cookie

JavaScript data storage Cookie, javascriptcookie

1. What is cookie?
A: cookies are used to store session information on the client.
2. What are the components of cookies?
① Name: a unique cookie name. It is recommended to be case sensitive. The cookie name must be URL encoded.
② Value: the string value stored in the cookie. The value must be URL encoded.
③ Domain: the domain in which the cookie is valid. All requests sent to this domain contain this cookie. This value can contain subdomains (such as www.wrox.com) or not (such as .wrox.com, which is valid for all subdomains of wrox.com ). If not explicitly set, this domain will be recognized as the domain from which the cookie is set.
④ Path: the path in the specified domain should send the cookie to the server. For example, you can specify that the cookie is accessible only from the http://www.wrox.com/books/, so the http://www.wrox.com page does not send cookie information, even if the request is from the same domain.
⑤ Expiration time: indicates the time stamp at which the cookie should be deleted. By default, all cookies are deleted when the browser session ends. However, you can set the deletion time on your own. This value is a date in GMT format (Wdy, DD-Mon-yyyy hh: MM: ss gmt), used to specify the exact time when the cookie should be deleted. Therefore, cookie
It can be stored on the user's machine after the browser is closed. If the expiration date you set is a previous time, the cookie will be deleted immediately.
⑥ Security flag: After a cookie is specified, it is sent to the server only when SSL connections are used. For example, cookie information can only be sent to the https://www.wrox.com, while requests from the http://www.wrox.com cannot send cookies.
3. list an example of a cookie?
The following is an example of a complete cookie. We will break it down:
Set-Cookie: name = value; expires = Mon, 22-Jan-07 07:10:24 GMT; domain = .wrox.com; path =/; secure
① Name: name String Representation
② Value: value String Representation
③ Expiration time: Mon, 22-Jan-07 07:10:24 GMT
④ Domain Name: .wrox.com
⑤ Path: Current directory/
⑥ Security flag: secure
4. How do I operate cookies?
 

Var CookieUtil = {// set cookie set: function (name, value, expires, domain, path, secure) {var cookieText = ""; cookieText + = encodeURIComponent (name) + "=" + encodeURIComponent (value); if (expires instanceof Date) {cookieText + = "; expires =" + expires. toGMTString ();} if (path) {cookieText + = "; path =" + path;} if (domain) {cookieText + = "; domain =" + domain ;} if (secure) {cookieText + = "; secure" ;}document. cookie = cookieText;}, // name = value; expires = expiration_time; path = domain_path; domain = domain_name; secure // get cookie get: function (name) {var cookieName = encodeURIComponent (name) + "=", cookieStart = document. cookie. indexOf (cookieName), cookieValue = ""; if (cookieStart>-1) {var cookieEnd = document. cookie. indexOf (";", cookieStart); if (cookieEnd =-1) {cookieEnd = document. cookie. length;} cookieValue = decodeURIComponent (document. cookie. substring (cookieStart + cookieName. length, cookieEnd);} return cookieValue;}, // Delete cookie unset: function (name, domain, path, secure) {this. set (name, "", Date (0), domain, path, secure) ;}}; // test CookieUtil. set ("name", "zhang"); var name = CookieUtil. get ("name"); alert (name); // zhang CookieUtil. unset ("name"); alert (CookieUtil. get ("name"); // null

5. cookie restrictions
① Limited data storage volume
② Information stored in cookies can only be accessed by approved recipients, but cannot be accessed by other domains
③ Limited security
6. For the small amount of cookie data storage problems, we propose the concept of sub-cookie. That is, multiple data entries are stored in the values of each cookie, separated.

Var SubCookieUtil = {/** set a complete cookie * param name: indicates the cookie name. Required * param subCookies: indicates the cookie value, which is an object, required * param expires: indicates the cookie expiration time. You can leave * param domain: indicates the cookie domain name. You can leave * param path: indicates the cookie path. You can leave * param secure: indicates the Security Flag of the cookie. You can leave it empty * eg: SubCookieUtil. setAll ("info", {name: "zhang", age: 23}); **/setAll: function (name, subCookies, expires, domain, path, secure) {var cookieText = "", subName, cookieParts = []; cookieText + = encodeURIComponent (name) + "="; for (subName in subCookies) {cookieParts. push (encodeURIComponent (subName) + "=" + encodeURIComponent (subCookies [subName]);} if (cookieParts. length> 0) {cookieText + = cookieParts. join ("&"); if (expires instanceof Date) {cookieText + = "; expires =" + expires. toGMTString ();} if (path) {cookieText + = "; path =" + path;} if (domain) {cookieText + = "; domain =" + domain ;} if (secure) {cookieText + = "; secure" ;}} else {cookieText + = "; expires =" + Date (0 ). toGMTString ();} document. cookie = cookieText;},/** set a cookie * param name: indicates the cookie name. Required: * param subName: indicates the name of the sub-cookie. Required: * param value: indicates the value of the sub-cookie. Required: * param expires: indicates the cookie expiration time. You can leave * param domain: indicates the cookie domain name. You can leave * param path: indicates the cookie path, you can leave * param secure blank to indicate the cookie security flag. You can leave * eg: SubCookieUtil empty. set ("info", "sex", "boy"); **/set: function (name, subName, value, expires, domain, path, secure) {var cookies = this. getAll (name) | |{}; cookies [subName] = value; this. setAll (name, cookies, expires, domain, path, secure) ;},/** read a complete cookie * param name: indicates the cookie name, required * return: A cookie object * eg: SubCookieUtil. getAll ("info"); **/getAll: function (name) {var cookieName = encodeURIComponent (name) + "=", cookieStart = document. cookie. indexOf (cookieName), cookieValue = "", I, len, subCookies, parts, result ={}; if (cookieStart>-1) {var cookieEnd = document. cookie. indexOf (";", cookieStart); if (cookieEnd =-1) {cookieEnd = document. cookie. length;} cookieValue = decodeURIComponent (document. cookie. substring (cookieStart + cookieName. length, cookieEnd); if (cookieValue. length> 0) {subCookies = cookieValue. split ("&"); for (I = 0, len = subCookies. length; I <len; I ++) {parts = subCookies [I]. split ("="); result [decodeURIComponent (parts [0])] = decodeURIComponent (parts [1]);} return result;} return null ;}, /** get the cookie value * param name: indicates the cookie name. Required: * param subName: indicates the name of the sub-cookie * return: the value of a sub-cookie * eg: subCookieUtil. get ("info", "name"); **/get: function (name, subName) {var cookies = this. getAll (name); if (cookies) {return cookies [subName];} else {return null ;},/** delete a complete cookie * param name: indicates the cookie name. * param domain: indicates the cookie domain name. You can leave * param path: indicates the cookie path. You can leave * param secure: indicates the cookie security flag, optional. * eg: SubCookieUtil. unsetAll ("info"); **/unsetAll: function (name, domain, path, secure) {this. setAll (name, "", Date (0 ). toGMTString (), domain, path, secure) ;},/** delete a cookie * param name: indicates the cookie name, required * param subName: indicates the name of the sub-cookie. * param domain: indicates the cookie domain name. You can leave * param path: indicates the cookie path. You can leave * param secure: indicates the cookie security flag, optional. * eg: SubCookieUtil. unset ("info", "name"); **/unset: function (name, subName, domain, path, secure) {var cookies = this. getAll (name); if (cookies) {delete cookies [subName]; this. setAll (name, cookies, null, domain, path, secure) ;}}; // test: var zhang = {name: "zhang", age: 23, height: "178", weight: "66 kg"} // set a complete cookie SubCookieUtil. setAll ("zhang", zhang); // obtain a complete cookie var zhang = SubCookieUtil. getAll ("zhang"); alert (zhang. weight); // 66 kg // Add a sub-cookie for the image. set ("zhang", "sport", "basketball"); // obtain the sub-cookie alert (SubCookieUtil. get ("zhang", "sport"); // basketball // delete a cookie SubCookieUtil. unset ("zhang", "age"); alert (SubCookieUtil. get ("zhang", "age"); // undefined // delete a complete cookie SubCookieUtil. unsetAll ("zhang"); alert (SubCookieUtil. getAll ("zhang"); // an error is returned because it has been deleted.

The above is all the content of this article, hoping to help you learn.

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