JavaScript to implement cookies write, read, delete function _javascript skills

Source: Internet
Author: User
Tags cookie names

Before I introduce the main body, we'll introduce the basics of cookies

First understand what a cookie is

"Cookies are variables that are stored on the computer of the visitor. This cookie is sent whenever the same computer requests a page through the browser. You can use JavaScript to create and retrieve a cookie's value. ”

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

From the JavaScript point of view, cookies are some string information. This information is stored on the client computer and is used to pass information between the client computer and the server.

This information can be read or set by Document.cookie in JavaScript. Because cookies are used for communication between the client and the server, the language of the server (such as PHP) can also access cookies in addition to JavaScript.

Cookie Basics

Cookies are limited in size, each cookie holds no more than 4kb of data, and if the length of the cookie string exceeds 4KB, the property returns an empty string.

Since cookies are ultimately stored as files on the client computer, it is convenient to view and modify cookies, which is why it is often said that cookies do not store important information.

The format of each cookie is the same: <cookie name >=< value >; name and value must be valid identifiers.

Cookies are valid for validity. By default, the life cycle of a cookie ends when the browser closes. If you want cookies to be available after the browser is turned off, you must set the expiration date for the cookie, which is the expiration of the cookie.

Alert (typeof Document.cookie) result is string, once I thought is array, also make joke ... 囧

Cookies have the concept of domain and path. Domains are the concept of domain, because browsers are security-aware environments, so different domains cannot access cookies between each other (and, of course, through a special set of cookies that are reachable across domains). The path is the concept of routing, a Web page created a cookie can only be with the Web page in the same directory or subdirectory access to all Web pages, and can not be the other directory access to the Web page (this sentence is a bit around, a look at an example is good to understand).

In fact, the way you create cookies is similar to the way you define variables, and you need to use cookie names and cookie values. The same Web site can create multiple cookies, and multiple cookies can be stored in the same cookie file.

Cookie FAQ

There are two types of cookies:

Cookies that you have browsed the current site itself set

Third-party cookies from other domain sources such as ads or pictures embedded on a Web page (Web sites can use these cookies to track your usage information)
The basics have just been about the cookie lifecycle, but cookies can be roughly divided into two states:

Cookies of a temporary nature. The current use of the site will store some of your personal information, when the browser is closed after the information will be removed from the computer
Set the cookie for the expiration time. Even if the browser shuts down, the information industry will still be on the computer. such as the login name and password, so you do not need to log on every time you visit a particular site. This cookie can be kept in a computer for days, months, or years.

There are two ways to clear cookies:

Using browser tools to clear cookies (with Third-party tools, the browser itself has this feature)
To clear cookies by setting the expiration date of a cookie

Note: Deleting cookies can sometimes cause some pages to not function correctly

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

For functional and performance reasons, it is recommended that you minimize the number of cookies used and use small cookies as much as possible.

Details about the cookie encoding will be presented separately in the cookie advanced article.

If it is a page on a local disk, the Chrome console is unable to use JavaScript to read and write the cookie, the solution ... Change to a browser ^_^.

This section shares a few simple actions about JavaScript for cookies, such as writing and deleting cookies.

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

A. Write Cookie:

Two parameters, one is the name of the cookie, one is the value
function Setcookie (name,value) {
 var days = 30;//This cookie will be saved 30 days
 var exp = new Date ();//new Date ("December, 9998");
 Exp.settime (Exp.gettime () + days*24*60*60*1000);
 Document.cookie = name + "=" + Escape (value) + "expires=" + exp.togmtstring ();
}

Two. Read cookies:

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

Three. Delete cookies:

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 Article

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.