JavaScript data store Cookie _javascript tips

Source: Internet
Author: User
Tags send cookies set cookie subdomain

1. What is a cookie?
A: Cookies are used to store session information on the client.
part of the 2.cookie?
① Name: A name that uniquely determines the cookie. It is recommended that the case be case-sensitive. The name of the cookie must be URL-coded.
② value: A string value stored in a cookie. The value must be encoded by the URL.
③ domain: Which domain the cookie is valid for. This cookie information will be included in all requests sent to the domain. This value can contain a subdomain (subdomain, such as www.wrox.com), or it may not contain it (such as. wrox.com, which is valid for all subdomains of wrox.com). If not explicitly set, the domain is considered the domain from which the cookie is set.
④ path: For the path in the specified domain, a cookie should be sent to the server. For example, you can specify that cookies can be accessed only from http://www.wrox.com/books/, so http://www.wrox.com pages will not send cookie information, even if the request is from the same domain.
⑤ Expiration Time: A timestamp that indicates when a cookie should be deleted. By default, all cookies are deleted at the end of the browser session, but you can also set the deletion time yourself. This value is a GMT-formatted date (Wdy, dd-mon-yyyy HH:MM:SS GMT) that specifies the exact time that the cookie should be deleted. Therefore, cookies
Can still be saved on the user's machine after the browser is closed. If you set the expiration date to be a previous time, the cookie will be deleted immediately.
⑥ Security flag: When specified, cookies are sent to the server only when using SSL connections. For example, cookie information can only be sent to https://www.wrox.com, and http://www.wrox.com requests cannot send cookies.
3. List An example of a cookie?
Here's an example of a complete cookie, which we'll break 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/
⑥ Safety sign: Secure
4. How do I manipulate cookies?
 

  var cookieutil = {//Set cookie Set:function (name, value, expires, domain, path, secure) {var cookiete
      XT = "";
      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) + "=", cookiest
      Art = 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, sec
    URE);
  }
  };
  Test Cookieutil.set ("name", "Zhang");
  var name = Cookieutil.get ("name");  alert (name);
  Zhang Cookieutil.unset ("name"); Alert (Cookieutil.get ("name"));
 Empty

5.cookie Restrictions
   ① Storage Data Quantity Limited
   ② stored in cookies The information in can only be accessed by approved recipients and cannot be accessed by other domains
   ③ security is limited
6. We propose the concept of a child cookie for storing a small amount of cookies data. That is, store multiple data in the value of each cookie, separated by "&".

  var subcookieutil = {/** sets a complete cookie * param name: Indicates the name of the cookie, required * param subcookies: Represents the value of a cookie, for a object, Required * param Expires: Indicates the expiration time of the cookie, can not fill * param domain: The domain name of the cookie, you can not fill * param path: The path of the cookie, you can not
    * Param secure: Indicates the security flag of the cookie, can not fill * EG:SUBCOOKIEUTIL.SETALL ("info", {name: "Zhang", age:23}); **/setall:function (Name, subcookies, expires, domain, path, secure) {var cookietext = "", SubName, Cookiepa
      RTS = [];
      Cookietext + = encodeURIComponent (name) + "="; For (SubName in subcookies) {Cookieparts.push (encodeURIComponent (subname) + "=" + encodeURIComponent (subCookies[su
      Bname]));
        } 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 child cookie * param name: Indicates the name of the cookie, required * param SubName: Represents the name of the child cookie, required * param value: Represents the child C Ookie value, required * param Expires: Indicates the expiration time of the cookie, can not fill * param domain: The domain name that represents the cookie, you can not fill * param path: The way to represent cookies
    diameter, you can not fill * param secure: To indicate the security of cookies, can not fill * eg:SubCookieUtil.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); /** reads a complete cookie * param name: Indicates the name of the cookie, required * Return: A Cookie Object * EG:SUBCOOKIEUTIL.GETALL ("in
    Fo "); **/getall:function (name) {var cookiename = EncoDeuricomponent (name) + "=", Cookiestart = Document.cookie.indexOf (cookiename), Cookievalue = "", I, Len, s
      Ubcookies, 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; /** gets the value of a sliver cookie * param name: Indicates the name of the cookie, required * param subname: The name of the child cookie * return: A child cookie's
    Value * Eg:SubCookieUtil.get ("info", "name");
    **/Get:function (name, subname) {var cookies = This.getall (name);
      if (cookies) {return cookies[subname];
      else {return null;  },/** deletes a complete cookie * param name: Indicates the name of the cookie, must fill * param domain: The domain name that represents the cookie, can not fill * param path
    : Indicates the path of the cookie, can not fill * param secure: The security symbol for cookies, you can not fill * EG:SUBCOOKIEUTIL.UNSETALL ("info"); **/unsetall:function (name, domain, path, secure) {This.setall (name, "", Date (0). togmtstring (), Domain, path
    , secure); /** Deletes a child cookie * param name: Indicates the name of the cookie, required * param subname: Indicates the name of the child cookie, required * param domain: means c Ookie domain name, you can not fill * param path: The path to the cookie, you can not fill * param secure: The security symbol for cookies, you can not fill * eg:SubCookieUtil.unse
    T ("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: "178cm", Weight: "66kg"}//Set up a complete cookie
  Subcookieutil.setall ("Zhang", Zhang);
  Get a complete cookie var Zhang = subcookieutil.getall ("Zhang");  alert (zhang.weight);
  66kg//Add a child cookie Subcookieutil.set to Zhang ("Zhang", "Sport", "basketball"); Gets the child cookie alert (Subcookieutil.get ("Zhang", "Sport"));
  Basketball//Delete a child cookie Subcookieutil.unset ("Zhang", "age");  Alert (Subcookieutil.get ("Zhang", "Age"));
  Undefined//Delete a complete cookie Subcookieutil.unsetall ("Zhang");  Alert (Subcookieutil.getall ("Zhang"));
 Error, because it has been deleted

The above is the entire content of this article, I hope 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.