Jquery.cookie.js--jquery's Cookie Plugin

Source: Internet
Author: User

A. js file

/*! * JQuery Cookie Plugin v1.4.1 *Https://github.com/carhartl/jquery-cookie* Copyright 2006, Klaus Hartl * released 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; function encode (s) {returnConfig.raw?S:encodeuricomponent (s); } function decode (s) {returnConfig.raw?S:decodeuricomponent (s); } function Stringifycookievalue (value) {returnEncode (Config.json?json.stringify (value): String (value)); } function Parsecookievalue (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) {}} function read (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); };}));

Ii. Description of Use

1. Introduction of JS file on the page

<script src= "Jquery.cookie.js" ></script><script src= "Jquery-1.11.1.min.js" ></script>

2. Write a cookie

Writing:

$.cookie ("Write cookie name", "cookie value written", {

Expires:7,

Path: "/",

Domain: "Address",

Secure:true

});

Parameter meaning:

Expires

Meaning: Validity period

Unit: Day

Writable values:

Number, write the number of times that this cookie is valid

Date object: Write a Date object directly as the cookie expires

Default: If you do not write or write null then the browser closes after the cookie is deleted

Path: The default is the page path that created the cookie

Domain: Name attribute, default is the page domain name that created the cookie

Secure: If true, the transfer of this cookie requires a security protocol such as HTTPS

3. Read cookies

notation: $.cookie ("read cookie name")

4. Delete Cookies

notation: $.cookie ("Deleted cookie name", NULL)

Parameter null: Represents the deletion of this cookie

.

Jquery.cookie.js--jquery's Cookie Plugin

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.