The cookie operation in JS

Source: Internet
Author: User

First, JS Cookie when used to introduce this code into the page

(function(Factory) {if(typeofdefine = = = ' function ' &&define.amd) {define (factory); } Else if(typeofExports = = = ' object ') {Module.exports=Factory (); } Else {        var_oldcookies =window.        Cookies; varAPI = window. Cookies =Factory (Window.jquery); Api.noconflict=function() {window. Cookies=_oldcookies; returnAPI;    }; }}(function () {    functionExtend () {vari = 0; varresult = {};  for(; i < arguments.length; i++) {            varattributes =arguments[i];  for(varKeyinchattributes) {Result[key]=Attributes[key]; }        }        returnresult; }    functionInit (Converter) {functionAPI (key, value, attributes) {varresult; //Write            if(Arguments.length > 1) {Attributes=Extend ({path:‘/‘}, Api.defaults, attributes); if(typeofAttributes.expires = = = ' Number ') {                    varexpires =NewDate (); Expires.setmilliseconds (Expires.getmilliseconds ()+ Attributes.expires * 864e+5); Attributes.expires=expires; }                Try{result=json.stringify (value); if(/^[\{\[]/. Test (Result)) {Value=result; }                } Catch(e) {} value=encodeURIComponent (String (value)); Value= Value.replace (/% (23|24|26|2b|3a|3c|3e|3d|2f|3f|40|5b|5d|5e|60|7b|7d|7c)/g, decodeuricomponent); Key=encodeURIComponent (String (key)); Key= Key.replace (/% (23|24|26|2b|5e|60|7c)/g, decodeuricomponent); Key= Key.replace (/[\ (\)]/g, escape); return(Document.cookie =[Key,=, value, Attributes.expires&& '; Expires= ' + attributes.expires.toUTCString (),//Use expires attribute, Max-age isn't supported by IEAttributes.path && '; Path= ' +Attributes.path, Attributes.domain&& '; Domain= ' +Attributes.domain, Attributes.secure? ‘; Secure ': '].join (‘‘)); }            //Read            if(!key) {Result= {}; }            //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 "Get ()"            varcookies = Document.cookie? Document.cookie.split ('; ‘) : []; varRdecode =/(%[0-9a-z]{2}) +/G; vari = 0;  for(; i < cookies.length; i++) {                varParts = cookies[i].split (' = ')); varName = Parts[0].replace (Rdecode, decodeuricomponent); varCookie = Parts.slice (1). Join (' = '); if(Cookie.charat (0) = = = ' "') {Cookie= Cookie.slice (1,-1); }                Try{Cookie= Converter && Converter (cookie, name) | |cookie.replace (Rdecode, decodeuricomponent); if( This. JSON) {                        Try{Cookie=json.parse (cookie); } Catch(e) {}}if(Key = = =name) {Result=cookies;  Break; }                    if(!key) {Result[name]=cookies; }                } Catch(e) {}}returnresult; } api.get= Api.set =API; Api.getjson=function () {            returnapi.apply ({json:true}, [].slice.call (arguments));        }; Api.defaults= {}; Api.remove=function(key, attributes) {API (key,‘‘, extend (attributes, {expires:-1            }));        }; Api.withconverter=Init; returnAPI; }    returninit ();}));

Set cookies

Cookies (name, value); | | Cookies.set (name, value);  Name: The names of the stored cookies; value: The values that correspond to the names
Cookies (name1, value1); | | Cookies.set (name1, value1);
Cookies (Name,value, {expires:7}); | | Cookies.set (Name,value, {expires:7});  Set the cookie expiration time, unit: Days
Cookies (Name,value, {expires:7,path: "/path", Domain: "domain name"}); | | Cookies.set (Name,value, {expires:7,path: "/path", Domain: "domain name"});//set cookies, including expiration path domain name, etc.

Read cookies

Cookies (); | | Cookies.get ();  Read all cookies, output object ==>{name:value, name1:value1}
Cookies (name); | | Cookies.get (name);  Read the cookie==>value of the corresponding name

Delete Cookies

Cookies (Name: ""); Empty the cookie
Cookie.remove (Name: ""); Delete cookie corresponding to cookie name also deleted

The cookie operation in JS

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.