The cookie and encapsulation in JS

Source: Internet
Author: User

Conceptual Reference Advanced Programming:

1. What is a cookie?

Cookies are information used for client storage sessions; format Key=value;

The composition of the 2.cookie

Name: The name of a unique cookie that is not case-sensitive, and the name of the cookie must be URL-encoded;

Value: The string stored in the cookie, the value must be URL encoded

Domain: A cookie is valid for which domain, and all requests sent to that domain contain this cookie information, which can contain subdomains or not contain this cookie information, and if not explicitly set, then the domain is considered to be the domain from which the cookie is set.

Path: For that path in the specified domain, you should send a cookie to the server, and you can specify that the cookie is accessible only from Http://www.person.com/mark, then http://www.person.com will not send cookie information, even if the request is from a domain

Expiry time: Timestamp when the cookie was deleted (when the cookie was stopped) by default, all cookies are deleted when the browser ends the session, but you can also set the delete time, which is the date in GMT format. Used to specify the standard time to delete the cookie,

Therefore, the cookie can be saved on the user's machine after the browser is closed and the cookie will be deleted immediately if the set expiration time is the previous time.

Each piece of information is used as part of the Set-cookie header, separated by semicolons and spaces.

Set-cookie:name=value; Expires=mon, 22-jan-07 07:10:24 GMT; Domain=.wrox.com

Operation of Cookies in 3.js: Read, write, delete, code as follows

varCookieutil ={set:function(name, value, Expiresday) {varExpiresdate =NewDate (); Expiresdate.setdate (Expiresdate.getdate ()+expiresday); Value=encodeURIComponent (value); varMyCookie = name + ' = ' + value + '; expires= ' +expiresdate; Document.cookie=MyCookie; }, get:function(name) {Name=encodeuricomponent (name); varCookielenght =document.cookie.length; vararr = Document.cookie.split ('; ‘);  for(vari = 0; i < arr.length; i++) {                varARR2 = arr[i].split (' = ')); if(Arr2[0] = =name) {                    returndecodeURIComponent (arr2[1]); }            }            return‘‘; }, remove:function(name) { This. Set (name, 1, 1); }    };

How the 4:cookie works

1). The client sends a message to the server via the browser, requesting a connection

2). The server establishes the Set-cookies Header based on the received request and returns it to the client

3). Client-side parsing returns content parsing and saving cookies

Limitations of 5:cookie

1). Only 10 cookies can be generated under each specific domain name

2). The maximum of the cookie is approximately 4096 bytes (4k), and for better compatibility, it is generally not more than 4095 bytes.

3). Cookies are stored in the client's text file, so particularly important and sensitive data is not recommended to be stored in cookies. Here is the question of security in mind.

Application of 4:cookie:

Cookies can be used to save data, such as a shopping cart, to save a username and password

The cookie and encapsulation in JS

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.