JavaScript realizes the write read and delete functions of cookie

  • 2020-09-27 00:58:49
  • OfStack

Before I go into the text, Let me give you the basics of Cookie

First, what is cookie

"cookie is a variable stored in the visitor's computer. This cookie is sent whenever the same computer requests a page through the browser. You can use JavaScript to create and retrieve cookie values."

cookie is a file created by a visited web site that is used to store browsing information, such as personal information.

From the point of view of JavaScript, cookie is 1 string information. This information is stored on the client's computer and is used to transfer information between the client's computer and the server.

In JavaScript, this information can be read or set via ES22en.cookie. Since cookie mostly communicates between the client and the server, in addition to JavaScript, the server language (such as PHP) can also access cookie.

Cookie basics

cookie has a size limit. Each cookie cannot hold more than 4kb. If the length of the cookie string exceeds 4kb, this property returns an empty string.

Since cookie ends up on the client computer as a file, it is easy to view and modify cookie, which is why it is often said that cookie does not hold important information.

Each cookie format looks like this: < cookie name > = < value > ; Both the name and the value must be valid identifiers.

cookie is valid. By default, the life cycle of 1 cookie ends when the browser is closed. If you want cookie to work even after the browser is closed, you must set an expiration date for the cookie, which is the expiration date for cookie.

alert(typeof document.cookie) turned out to be string. Once I thought it was array. �

cookie has the concept of fields and paths. The domain is the concept of domain, and because the browser is a secure environment, different domains cannot access cookie to each other (cookie can be cross-domain access by special Settings, of course). The path is the concept of routing. The cookie created by a page can only be accessed by all pages in the same directory or subdirectory with the page, but not by pages in other directories.

In fact, cookie is created in a similar way to how variables are defined, using the cookie name and the cookie value. Multiple cookie can be created on the same website, and multiple cookie can be stored in the same cookie file.

Frequently asked questions

There are two types of cookie:

The current site you visit itself is set to cookie

Third party cookie from other domain sources such as embedding ads or images on web pages (the site can track your usage by using these cookie)
As mentioned in the basic knowledge about the LIFE cycle of cookie, cookie can be roughly divided into two states:

Temporary nature cookie. The site stores some of your personal information while you are using it, and when the browser is closed, the information is deleted from the computer
Set cookie for expiration time. Even if the browser is shut down, the information will still be on the computer. Such as a login name and password, so that you do not have to log in every time you visit a particular site. The cookie can be kept on computers for days, months or even years.

cookie has two cleaning methods:

Clear cookie with the browser tool (there is a third party tool and the browser itself has this capability)
Clear cookie by setting the expiration date of cookie

Note: Removing cookie may sometimes cause some web pages to fail

The browser can be set to accept and deny access to cookie.

For functional and performance reasons, it is recommended to minimize the number of cookie and to use smaller cookie whenever possible.

The details of THE cookie encoding will be covered separately in the cookie Advanced article.

If it is a page on a local disk, chrome's console cannot read or write cookie, the solution... Change 1 browser ^_^.

This section shares a few paragraphs on javascript's simple operations with cookie, such as writing and deleting cookie.

The code is very simple, more suitable for cookie basic operation is not very skilled reference friends.

Write cookie: 1.


// Two parameters, 1 One is cookie The name, 1 Is the value 
function SetCookie(name,value){
 var Days = 30;// this  cookie  Will be saved  30  day 
 var exp = new Date();//new Date("December 31, 9998");
 exp.setTime(exp.getTime() + Days*24*60*60*1000);
 document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}

2. Read cookie:


// take cookies function   
function getCookie(name){
 var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
 if (arr != null) return unescape(arr[2]); return null;
}

3. Delete cookie:


// delete cookie
function delCookie(name){
 var exp = new Date();
 exp.setTime(exp.getTime() - 1);
 var cval = getCookie(name);
 if (cval != null) document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}

Related articles: