jquery Operation Cookie Plugin sharing

Source: Internet
Author: User

  In the Web client, we often encounter reading or setting cookies, if the use of pure JS we may encounter some compatibility problems, here to introduce a more practical jquery operation Cookies Plug-ins

The source code for the plugin is as follows:   code is as follows: Jquery.cookie = function (name, value, options) {    if (typeof value!= ' undefined ') {         options = Options | | {};         if (value = = null) {            value = ';     &NBS P       options = $.extend ({}, Options);             options.expires =-1;        }         var expires = ';         if (Options.expires && (typeof options.expires = ' number ' | | options.expires.toUTCStr ing) {            var date             if (typeof OPTIONS.E Xpires = = ' number ') {                date = new Date ()       &NBS P         Date.settime (Date.gettime () + (Options.expires * 24 * 60 * 60 * 1000));           &NBsp else {                date = Options.expires &nbsp         ;  }             expires = '; Expires= ' + date.toutcstring ();        }         var path = Options.path? '; Path= ' + (Options.path): ';         var domain = Options.domain? '; Domain= ' + (options.domain): ';         var secure = options.secure? '; Secure ': ';         Document.cookie = [name, ' = ', encodeURIComponent (value), expires, path, domain, Secure].join (' ');    } else {        var cookievalue = null;         if (document.cookie && document.cookie!= ') {            var cookies = Document.cookie.split (';'); nbsp           for (var i = 0; i < cookies.length i++) {        &NBSp       var cookie = Jquery.trim (Cookies[i]);                 if (cookie.substring (0, name.length + 1) = = (name + ' = ')) {                    Cookievalue = decodeuricomponent (cookie.substring name.len Gth + 1));                     break;                            }        }         return cookievalue;    }};     Specific use is as follows: 1, set the value of the cookie, such as we want to set variable name userid corresponding value of 123 cookies, the code is as follows: $.cookie (' userid ', ' 123 '); 2, create a new cookie, and set the duration of the cookie path domain name, and so on, the code is as follows: $.cookie (' userid, ' 123 ', {expires:7, path: '/', Domain: ' jquery.com ', Secure:tru e}); Note: If you remove the parameters for the following {}, the new will take effect with the default settings. 3, delete the cookie, that is, the corresponding cookie value to NULL, the code is as follows: $.cookie (' userid ', null); 4, read cookies, such as reading the variable name userid cookie value, the code is as follows: Var uid= $.cookie (' userid '); How about, using jquery to manipulate cookies is notVery simple, you do not have to write too many JS, also do not have to worry about those compatibility, everything is so smooth.  

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.