Cookie manipulation in jquery

Source: Internet
Author: User

Introduce the following code in the page before use

/*! * JQuery Cookie Plugin v1.4.1 * Https://github.com/carhartl/jquery-cookie * * Copyright 2006, Klaus Hartl * Rel Eased under the MIT license*/(function(Factory) {if(typeofdefine = = = ' function ' &&define.amd) {//AMD (Register as an anonymous module)define ([' jquery '], factory); } Else if(typeofExports = = = ' object ') {        //Node/commonjsModule.exports = Factory (Require (' jquery '))); } Else {        //Browser GlobalsFactory (JQuery); }}(function ($) {    varpluses =/\+/G; functionencode (s) {returnConfig.raw?S:encodeuricomponent (s); }    functionDecode (s) {returnConfig.raw?S:decodeuricomponent (s); }    functionStringifycookievalue (value) {returnEncode (Config.json?json.stringify (value): String (value)); }    functionParsecookievalue (s) {if(S.indexof (' "') = = = 0) {            //This was a quoted cookie as according to RFC2068, unescape ...s = S.slice (1,-1). replace (/\\ "/g, '" '). Replace (/\\\\/g, ' \ \ ')); }        Try {            //Replace server-side written pluses with spaces.            //If We can ' t decode the cookie, ignore it, it ' s unusable.            //If We can ' t parse the cookie, ignore it, it ' s unusable.s = decodeuricomponent (S.replace (pluses, ")); returnConfig.json?Json.parse (s): s; } Catch(e) {}}functionread (s, converter) {varValue = Config.raw?S:parsecookievalue (s); return$.isfunction (Converter)?Converter (value): value; }    varConfig = $.cookie =function(key, value, options) {//Write        if(Arguments.length > 1 &&!)$.isfunction (value)) {Options=$.extend ({}, Config.defaults, options); if(typeofOptions.expires = = = ' Number ') {                vardays = options.expires, t = Options.expires =NewDate (); T.setmilliseconds (T.getmilliseconds ()+ Days * 864e+5); }            return(Document.cookie =[Encode (key),=, Stringifycookievalue (value), Options.expires? ‘; Expires= ' + options.expires.toUTCString (): ",//Use expires attribute, Max-age isn't supported by IEOptions.path? ‘; Path= ' + options.path: ', Options.domain? ‘; Domain= ' + options.domain: ', Options.secure? ‘; Secure ': '].join (‘‘)); }        //Read        varresult = key?undefined: {},//To prevent the as loop in the first place assign an empty array            //In case There is no cookies at all. Also prevents odd result when            //calling $.cookie ().cookies = Document.cookie? Document.cookie.split ('; ‘): [], I= 0, L=cookies.length;  for(; i < L; i++) {            varParts = cookies[i].split (' = ')), name=Decode (Parts.shift ()), Cookie= Parts.join (' = ')); if(Key = = =name) {                //If second argument (value) is a function it ' s a converter ...result =Read (cookie, value);  Break; }            //Prevent Storing a cookie that we couldn ' t decode.            if(!key && (cookie = read (cookie))!==undefined) {Result[name]=cookies; }        }        returnresult;    }; Config.defaults= {}; $.removecookie=function(key, options) {//must not alter options, thus extending a fresh object ...$.cookie (Key, ' ', $.extend ({}, Options, {expires:-1 })); return!$.cookie (key); };}));

Set cookies

1.$.cookie (CookieName, cookievalue); CookieName: The cookie name to be set, Cookievalue represents the corresponding value.



Note: When a cookie is specified, the cookie created is referred to as a "persistent cookie (persistent cookie)".

3.$.cookie (' The_cookie ', ' The_value ', {expires:7, path: '/'}); Create a cookie and set a valid path for the cookie:



The path is set to the root of the Web site, allowing all pages to read cookies to each other (typically not set to prevent conflicts)

Read cookies

$.cookie (//  

Delete Cookies

NULL ); $.cookie (

Cookie manipulation in jquery

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.