A cookie is a small piece of user information that a Web server holds in a text file on a user's computer, and whenever a user accesses a Web server, the relevant cookie stored on the user's computer is read by the client to the server side, and the server is set up to serve the user based on the cookie information. At the same time, a cookie is a mechanism provided by the browser that provides the cookie attribute of the document object to JavaScript, which can be controlled by JavaScript.
I. Overview of Cookies
Cookies are temporary and permanent, and temporary cookies are only available for the current browser session, and a permanent cookie will automatically generate a text file on the client's computer, so it can be used outside of the browser.
1. Form
A cookie has 5 properties:
Name: The only property that must be set, which represents the name of the cookie.
Expires: Specifies how long the cookie will remain on the client before it is deleted. If you do not set this property, the cookie will automatically disappear when the user closes the current browser.
Path: Determines the availability of cookies for other pages on the server, and cookies are generally available for all pages in the unified directory. When this property is set, the cookie detachment specifies the path and all pages under the sub-path are valid.
Domain: A multi-state server that primarily sets the same domain shares a cookie.
Secure: Indicates that a cookie can only be transmitted by using an Internet connection that uses HTTPS or other security protocols. If you do not set this property, the cookie is sent unencrypted on the network.
To set the syntax for a cookie:
Set-cookie:name = value; [expirse = Date]; [path = dir]; [domain = domainname]; [Secure]
2. Use
A cookie is a method of maintaining Web server continuity that helps a Web server keep information about a visitor. When a user requests a page, the cookie can help the server identify the user, so it is often used to save user login status, track user behavior, create shopping carts, conduct polls, etc.
3. Pros and cons
The advantage of the cookie is its persistence, it can save the user state and access information, when the user returns to the site each time, the page set more user-friendly, but it can users in a site has been placed on the page and the number of times, viewed ads and so on. Another advantage is the ability to write functions that manipulate cookies in JavaScript, making it easy to manipulate cookies in the JavaScript language, combining them to make the Web page more dynamic.
Its shortcomings mainly focus on security, cookies can be disabled and deleted, and different browser cookies are not shared, so cookies between different browsers are not accessible to each other.
Ii. use of cookies in JavaScript
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Use of cookies