Home >Web Front-end >JS Tutorial >Basic operations of adding and deleting cookies in JavaScript

Basic operations of adding and deleting cookies in JavaScript

黄舟
黄舟Original
2017-07-24 15:51:301411browse

This article mainly introduces relevant information on the basic operations of javascript cookies. Here is a detailed introduction to the operation of Cookies including obtaining, creating, setting the validity period, adding and deleting, etc. Friends who need it can refer to it

Basic operations of javascript cookie (add and delete)

1. Add a cookie: response.addCookie(Cookie c);

2. Get the cookie collection: request.getCookies();

3. Create a new cookie:


new Cookie(String name,String value) — Cookie在构造的时候就需要设定好cookie的名字和值

4. Get the name of the cookie: cookie.getName();

5. Get the value of the cookie: cookie.getValue();

6. Set the value of the cookie: cookie.setValue();

Setting and obtaining the cookie validity period

cookie.setMaxAge() and cookie.getMaxAge() methods

– If a Cookie has not set MaxAge, the Cookie is a temporary Cookie; browser When the browser memory is destroyed, the cookie information disappears.

If the cookie is also set to MaxAge, the browser will save the cookie information in the browser's temporary file in the form of a file. folder, you can see the cookie file in the browser's temporary folder, so the cookie information exists before the cookie expires;

Delete a Cookie

Send a cookie with the same name and path (the browser identifies the cookie by name + path);

maxAge is set to 0 (the cookie times out and is deleted immediately after overwriting);

setPath() and getPath() methods

are used to set the path and its sub-paths when accessing the server with the current cookie information;

setDomain() and getDomain() methods

are used to notify the browser to bring the current cookie information when accessing which domain name. (ps: Current browsers do not allow cookie settings Pass the domain information, otherwise it will automatically refuse to receive this cookie.)

The above is the detailed content of Basic operations of adding and deleting cookies in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn