Introduced
A cookie is a mechanism provided by the browser that provides the cookie properties of the document object to JavaScript. It can be controlled by JavaScript, not by the nature of JavaScript itself. A cookie is a file that is stored on the user's hard drive, which usually corresponds to a domain name, which is made available when the browser accesses the domain name again. Therefore, cookies can span multiple pages under a domain name, but cannot be used across multiple domain names.
What is a cookie
A cookie is a mechanism that a browser provides to a client to store a small amount of data. It is convenient to refer to another page's data in one page, either client script or server-side script. A cookie is one of the few persistent objects that a browser provides to JavaScript. The so-called persistence object refers to the life cycle of the browser window object.
Client-Side Cookies
The cookie property of the Document object can manipulate the browser's cookie object, which is a singleton pattern, which means that it is unique in the browser instance and that two completely different Windows access an instance
JavaScript notifies the browser to create and delete cookies by setting the description of the string. (Cookies have long been a controversial topic and have been criticized by many network experts for their safety concerns).
Properties of the cookie
The browser's cookie object can be viewed as an associative array of key-value. There are two required properties for name and value, and 4 optional attributes, namely lifetime, visibility, and security.
(1) lifetime (expires). By default, the value stored by the cookie is only present during the browser session, and when the user closes the browser, the values are destroyed and if the cookie is to be present longer than the session, a long expiration date can be specified by expires. Just before this date, the cookie is there.
(2) The Visible Path (path). Specifies a Web page that is associated with a cookie that can be accessed only under a set path, and if you want to allow access to this cookie from any page in a server domain, set his path to "/".
(3) Visible domains (domain). Like most browser persistence objects, cookies follow the "same-origin policy" because of security factors. Therefore, cookies created on a server domain can only be accessed in this domain. By default, the value of domain is the host name of the server where the cookie is created, and the domain is set up as a subdomain to restrict the cookie from being accessible only under a subdomain. However, the browser does not allow the cookie to be set to a domain other than the server's domain.
(4) security. is a Boolean value that specifies how the network cookie is transmitted, and the default secure value is false, which means that it can be transmitted through a normal, unsecured HTTP connection. But if set to true, Then the cookie will only be transmitted in the browser via HTTPS or other security protocols.
Operation of Cookies
<script type= "Text/javascript" ><!--varCookie = { //Set CookiesSetcookie:function(name, value, expires, path, domain) {document.cookie= name + "=" + Escape (value) +((expires)? "; expires=" + expires.togmtstring (): "") +(PATH)? ";p ath=" + Path: "") +(domain)? ";d omain=" + Domain: "" ); }, //Get CookiesGetCookie:function(name) {vararr = Document.cookie.match (NewRegExp ("(^|)" + name + "= ([^;] *)(;|$)")); if(Arr! =NULL)returnUnescape (arr[2]);return NULL; }, //Delete CookiesDelcookie:function(name) {varD =NewDate (); D.settime (D.gettime ()-3600 * 1000); This. Setcookie (Name, "", D); }} Cookie.setcookie ("A", 34);//set the cookie to name:a; value:34Cookie.delcookie ("a")//Delete a cookie with name aAlert (Cookie.getcookie ("a"));//returns NULL because the cookie has been deleted// -</script>
Detailed explanation
Suggested--http://www.cnblogs.com/chengxiaohui/articles/1869244.html
L--cookie