Use of jquery cookies (delete, add, modify)

Source: Internet
Author: User
Tags getdate

The browser's cookie generally has the following four customizable properties:
1. Expires
Expiration time, that is, the time the cookie died.
If a cookie is not expires, the cookie disappears after the browser closes.
Cookies can be kept alive until expires time, and automatically expire and disappear after expiration.
2. Path
3. Domain
4. Secure

The code is as follows Copy Code
$.cookie (' The_cookie '); Read Cookie value
$.cookie (' The_cookie ', ' the_value '); Set the value of a cookie
$.cookie (' The_cookie ', ' The_value ', {expires:7, path: '/', Domain: ' jquery.com ', secure:true});/create a new cookie including

Validity path domain name, etc.

The code is as follows Copy Code
$.cookie (' The_cookie ', ' the_value '); New Cookie
$.cookie (' The_cookie ', null); Delete a cookie

We'll write it in a way.

The code is as follows Copy Code
var Cookie = {
Set:function () {
var name = Arguments[0],
Value = Escape (Arguments[1]),
Days = (Arguments.length > 2)? ARGUMENTS[2]: 365,
Path = (Arguments.length > 3)? ARGUMENTS[3]: "/";
With (new Date ()) {
Setdate (getDate () + days);
Days = toUTCString ();
}
Document.cookie = "{0}={1};expires={2};p ath={3}". Format (name, value, days, path);
},
Get:function () {
var returnvalue = Document.cookie.match (New RegExp ("[b^;]?" + arguments[0] + "= ([^;] *) (? =;|b|$) "," I ");
Return returnvalue? Unescape (Returnvalue[1]): returnvalue;
},
Delete:function () {
var name = Arguments[0],
Value = Escape (""),
Days = (Arguments.length > 2)? ARGUMENTS[2]: 365,
Path = (Arguments.length > 3)? ARGUMENTS[3]: "/";
With (new Date ()) {
Setdate (getDate () + days);
Days = toUTCString ();
}
Document.cookie = "{0}={1};expires={2};p ath={3}". Format (name, value, days, path);
}
}

expires:365
Defines the valid time for a cookie, which can be a number (from the time the cookie was created, in days), or a Date object. If omitted, the cookie created is a session cookie that will be deleted when the user exits the browser.

Path: '/'
By default: Only pages that have a cookie set can read the cookie.
Defines a valid path for a cookie. By default, the value of the parameter is the path to the Web page where the cookie was created (the behavior of the standard Viewer). If you want to access this cookie throughout your Web site, you need to set a valid path: path: '/'. If you want to delete a cookie that defines a valid path, you need to include this path when calling the function: $.cookie (' R9it ', Null,{path: '/'});.

Domain: ' example.com '
Default value: The domain name of the Web page that created the cookie.

Secure:true
Default value: False. If the transport for True,cookie requires the use of security Protocol (HTTPS).

Raw:true
Default value: False.
By default, encoding and decoding is done automatically when the cookie is read and written (using encodeURIComponent encoding, decodeuricomponent decoding). To turn off this feature setting raw:true.

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.