JavaScript cookies
Learning Essentials:
Cookie Introduction
Create and get cookies
Encoding of Cookies
The lifetime of a cookie
Path of the cookie
Domain of Cookies
Secure the cookie
I. Introduction of Cookies
A cookie is actually some information that is stored on the client computer as a file. In JavaScript, cookies are used primarily to preserve state or to identify identities.
Ii. creation and acquisition of cookies
The syntax code for creating a cookie is as follows: Document.cookie= "Name=value"
The syntax code for getting a cookie is as follows: Document.cookie
Third, the encoding of the cookie
Use the Escape () function to encode the cookie value and restore it by using the unescape () function when reading the cookie.
Iv. the lifetime of a cookie
By default, when the browser is closed, the cookie disappears. If you want to continue to use it after the browser window, you need to set a lifetime for the cookie.
To set the lifetime of a cookie, you can use the following code:
Document.cookie=name=value;expires=date;
Date format: Wdy,dd-mon-yy HH:MM:SS GMT
V. Path to cookies
By setting up path=/to achieve the purpose of accessing cookies throughout the site.
Vi. Domain of Cookies
Cookies can be used to resolve the issue of cookie access in different virtual directories, but path does not address the issue of accessing cookies in different domains.
such as: Www.aspxfans.com and booksupport.aspxfans.com, set the domain of the cookie to. aspxfans.com just fine.
Vii. Secure the cookie
By default, cookies use a normal, unencrypted HTTP transport method. After setting the secure cookie, the cookie can only be transmitted over HTTPS or under other security protocols.
Basic JavaScript Learning (15)