Click “like” and then look, wechat search [Big Move the world] pay attention to this person without dACHang background, but with a positive attitude upward. In this paper, making github.com/qq449245884… Has been included, the article has been categorized, also organized a lot of my documentation, and tutorial materials.

Everyone said there was no project on your resume, so I found one and gave it away【 Construction tutorial 】.

Cookies provide a useful way for Web applications to store user-related information. For example, when a user visits our site, cookies can be used to save user preferences or other information so that the application can retrieve previously saved information the next time the user visits our site.

What the hell is a Cookie

A Cookie is a small piece of text that passes between the Web server and the browser along with a user request and page. Each time a user visits the site, the Web application can read the information contained in the Cookie.

Cookies were invented to solve the problem of saving user information. For example,

  • When a user visits a web page, the user’s name can be stored in a cookie.
  • The cookie remembers the user name the next time the user visits the page.

Cookies remember the user’s information across all web pages. It contains information as a string and is stored as key-value pairs, in the form of key=value. Cookies are usually preceded by a “;” Space.

username = Daisy Green
Copy the code

Cookie shortcomings

  • cookieMay be disabled. If a user is concerned about privacy, he is likely to disable cookies in his browser.
  • cookieIs browser-specific. This means that even if you’re visiting the same page, it’s saved between different browserscookieThey can’t visit each other;
  • cookieMay be deleted. Because eachcookieIs a file on the hard disk, so is likely to be deleted by the user;
  • cookieSecurity is not high enough. All of thecookieAll files are recorded in plain text. Therefore, if you want to save information such as user names and passwords, you had better encrypt them in advance.

Cooke’s way of working

The server sends some data to the visitor’s browser in the form of cookies. If the browser allows you to accept cookies. It is stored as a plain text record on the visitor’s hard drive.

When a visitor jumps to another page, the browser sends the same cookie to the server for retrieval. Once it is retrieved, your server knows or remembers what was stored previously.

The composition of the Cookie

Cookie In the HTTP Header information, the HTTP set-cookie Header format is as follows:

Set-Cookie: name=value; [expires=date]; [path=path];
[domain=domainname]; [secure];
Copy the code

A concrete example in HTTP code:

<meta http-equiv="set-cookie" content=" cookieName = cookieValue;
expires=01-Dec-2006 01:14:26 GMT; path=/" />
Copy the code

As you can see from the format above, a Cookie consists of the following parts.

The Name/Value pairs

Name and Value are separated by semicolons (;). A maximum of 20 pairs of cookies exist. Each web page has a maximum of one Cookie. For Value values, it is best to encode them with encodeURIComponent.

Domain

Domain The Domain name is also a part of the Cookie. By default, the Domain name of the web page accessed by the user is stored in the Cookie. If you set the domain name value of this Cookie, it means that all servers in the domain name, not just the server you are accessing, can access the Cookie. Normally, do not do this. Set the domain name format as follows: domain=http://xyz.com

path

Set the path format for which pages in a directory are accessible to cookies for a particular server: path = /movies

Expires

By default, the Cookie will be automatically deleted when the user closes the browser. If the Cookie expiration time is not set, the Cookie will disappear when the user closes the browser. If set, you can extend the lifetime of the Cookie. Set the time in JS to the GMT form of the Date object in the following format: Expires = date.togmtString ()

Secure

Take the value true or false. If true, then cookies must be sent over HTTPS.

JS Cookie

In JS, cookies can be manipulated using the cookie property of the Document object. Javascript can read, create, modify and delete the cookie of the current page, to see the specific operations.

Create a Cookie

JS can create cookies using the document.cookie property. Cookies can be created by:

document.cookie = "username=Daisy Green";
Copy the code

You can also add a valid date (UTC time). By default, cookies are deleted when the browser is closed:

document.cookie = "username=Daisy Green; expires=Mon, 26 Aug 2019 12:00:00 UTC";
Copy the code

The path parameter tells the browser what path the cookie belongs to. By default, cookies belong to the current page.

document.cookie = "username=Daisy Green; expires=Mon, 26 Aug 2019 12:00:00 UTC"; path=/";
Copy the code

Read the Cookie

With JS, cookies can be read like this:

var x = document.cookie;
Copy the code

Document. cookie returns all cookies in a string, for example: cookie1=value; cookie2

Example:

<html> <head> <script type = "text/javascript"> <! -- function ReadCookie() { var allcookies = document.cookie; document.write ("All Cookies : " + allcookies ); // Get all the cookies pairs in an array cookiearray = allcookies.split('; '); // Now take key value pair out of this array for(var i=0; i<cookiearray.length; i++) { name = cookiearray[i].split('=')[0]; value = cookiearray[i].split('=')[1]; document.write ("Key is : " + name + " and Value is : " + value); } } //--> </script> </head> <body> <form name = "myform" action = ""> <p> click the Button to View Result:</p> <input type = "button" value = "Get Cookie" onclick = "ReadCookie()"/> </form> </body> </html>Copy the code

Run:

Change the cookie

By using JS, we can change it like a cookie:

document.cookie = "username=Steve Jobs; expires=Sun, 31 Dec 2017 12:00:00 UTC; path=/";
Copy the code

The old cookie will be overwritten.

Example:

<html> <head> <script type = "text/javascript"> <! -- function WriteCookie() { var now = new Date(); now.setMonth( now.getMonth() + 1 ); cookievalue = escape(document.myform.customer.value) + ";" document.cookie = "name=" + cookievalue; document.cookie = "expires=" + now.toUTCString() + ";" document.write ("Setting Cookies : " + "name=" + cookievalue ); } //--> </script> </head> <body> <form name = "myform" action = ""> Enter name: <input type = "text" name = "customer"/> <input type = "button" value = "Set Cookie" onclick = "WriteCookie()"/> </form>  </body> </html>Copy the code

Run:

Delete the cookie

Deleting a cookie is very simple. You don’t have to specify a cookie value: Simply set the expires parameter to a past date:

document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;" ;Copy the code

The cookie path should be defined to ensure that the correct cookie is deleted. Some browsers won’t let us delete cookies if we don’t specify a path.

Example:

<html> <head> <script type = "text/javascript"> <! -- function WriteCookie() { var now = new Date(); now.setMonth( now.getMonth() - 1 ); cookievalue = escape(document.myform.customer.value) + ";" document.cookie = "name=" + cookievalue; document.cookie = "expires=" + now.toUTCString() + ";" document.write("Setting Cookies : " + "name=" + cookievalue ); } //--> </script> </head> <body> <form name = "myform" action = ""> Enter name: <input type = "text" name = "customer"/> <input type = "button" value = "Set Cookie" onclick = "WriteCookie()"/> </form>  </body> </html>Copy the code

The bugs that may exist after code deployment cannot be known in real time. In order to solve these bugs, I spent a lot of time on log debugging. Incidentally, I recommend a good BUG monitoring tool for youFundebug.

Reference: www.w3schools.com/js/js_cooki…

communication

This article is updated every week, you can search wechat “big move the world” for the first time to read and urge more (one or two earlier than the blog hey), this article GitHub github.com/qq449245884… It has been included and sorted out a lot of my documents. Welcome Star and perfect. You can refer to the examination points for review in the interview.