Cookies are valid.By default,The life cycle of a cookie ends when the browser is closed,This article introduces you to the simple operation of javascript for cookies.For example, for writing and deleting cookies,The code is simple and easy to understand,

Before introducing the text,Let me introduce you to the basics of cookies

First understand what cookies are

"Cookies are variables stored on a visitor's computer.Whenever the same computer requests a page through a browser,This cookie will be sent. You can use javascript to create and retrieve cookie values. "

Cookies are files created by visited websites,Used to store browsing information,Such as profile information.

From a javascript perspective,A cookie is just some string information.This information is stored on the client's computer,Used to pass information between the client computer and the server.

JavascriptIn JavaScript, you can read or set this information through document.cookie.Because cookies are used for communication between the client and the server,So in addition to javascript, server-side languages ​​(such as php) can also access cookies.

cookie basics

Cookies are limited in size.The data stored in each cookie cannot exceed 4kb. If the length of the cookie string exceeds 4kb, this property will return an empty string.

Because cookies are ultimately stored on the client computer as files,So it's very convenient to view and modify cookies,This is why it is often said that cookies cannot store important information.

The format of each cookie is this:<cookie name>=<value>Both the name and value must be legal identifiers.

Cookies are valid.By default,The life cycle of a cookie ends when the browser is closed.If i want cookies to work even after your browser is closed,You must set an expiration date for the cookie,This is the expiration date of the cookie.

alert (typeof document.cookie) turned out to be a string. I used to think it was an array, and I made a joke...Oops

Cookies have the concept of domains and paths.Domain is the concept of domain, because the browser is a secure environment,Therefore, cookies cannot be accessed between different domains (of course, cookies can be accessed across domains through special settings). Path is the concept of routing. The cookie created by a webpage can only be accessed by all webpages in the same directory or subdirectory as this webpage.And cannot be accessed by webpages in other directoriesIt's easy to understand by looking at an example in a while).

In fact, the way to create cookies is similar to the way to define variables.Both require a cookie name and cookie value. The same website can create multiple cookies, and multiple cookies can be stored in the same cookie file.

cookie faq

There are two types of cookies:

The cookie you set on the current website you are browsing

Third-party cookies from other domain sources, such as embedding ads or images on the webpage (the website can track your usage information by using these cookies)

Just now the basics talk about the problem of cookie life cycle,In fact, cookies can be roughly divided into two states:

Cookies of a temporary nature. The website will store some of your personal information during the current use.This information is also deleted from the computer when the browser is closed

Set a cookie for expiration time. Even if the browser is closed,These information industries will still be in the computer.Such as login name and password,This eliminates the need to log in every time you visit a particular site.Such cookies can stay on your computer for days, months, or even years.

There are two ways to clear cookies:

Clear cookies with browser tools (there are third-party tools,(The browser itself also has this feature)

Clear cookies by setting their expiration date

Note:Deleting cookies can sometimes cause some webpages to malfunction

Browsers can be set to accept and deny access to cookies.

For functional and performance reasons,We recommend minimizing the use of cookies.And try to use small cookies.

Details about cookie encoding will be covered separately in the advanced section of cookies.

If it is a page on the local disk,The Chrome console cannot read and write cookies with JavaScript. The solution... another browser ^ _ ^.

This section shares a few paragraphs about the simple operation of javascript for cookies.For example, writing and deleting cookies.

The code is simple,More suitable for friends who are not too familiar with basic cookie operations.

Write cookies:

//Two parameters,One is the name of the cookie and the other is the value
function setcookie (name, value) {
 var days=30;//this cookie will be saved for 30 days
 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 ();

Read cookies:

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

3. Delete cookies:

//Delete cookies
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 ();
  • Previous The use of the more commonly used DateTime structure in C #
  • Next Deep understanding of closures in Swift