JQuery Cookie operation plug-in

Source: Internet
Author: User

Cookie plug-in of jQuery

01 // jQuery.cookie.js
02 jQuery.cookie = function(name, value, options) {
03     if (typeof value != 'undefined') {
04         options = options || {};
05         if (value === null) {
06             value = '';
07             options = $.extend({}, options);
08             options.expires = -1;
09         }
10         var expires = '';
11         if (options.expires && (typeof options.expires == 'number'|| options.expires.toUTCString)) {
12             var date;
13             if (typeof options.expires == 'number') {
14                 date = new Date();
15                 date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
16             else {
17                 date = options.expires;
18             }
19             expires = '; expires=' + date.toUTCString();
20         }
21         var path = options.path ? '; path=' + (options.path) : '';
22         var domain = options.domain ? '; domain=' + (options.domain) : '';
23         var secure = options.secure ? '; secure' '';
24         document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
25     else {
26         var cookieValue = null;
27         if (document.cookie && document.cookie != '') {
28             var cookies = document.cookie.split(';');
29             for (var i = 0; i < cookies.length; i++) {
30                 var cookie = jQuery.trim(cookies[i]);
31                 if (cookie.substring(0, name.length + 1) == (name + '=')) {
32                     cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
33                     break;
34                 }
35             }
36         }
37         return cookieValue;
38     }
39 };

 

 

The usage is as follows:
1. Set the cookie value. For example, to set the cookie with the variable userid 123, the Code is as follows:
$. Cookie ('userid', '20140901 ');
2. Create a cookie and set the Domain Name of the cookie's validity period. The Code is as follows:
$. Cookie ('userid, '000000', {expires: 7, path: '/', domain: 'jquery. com', secure: true });
Note: If the {} parameter is removed, it will take effect with the default setting after the new parameter is created.
3. delete a cookie. Set the cookie value to null. The Code is as follows:
$. Cookie ('userid', null );
4. Read the cookie. For example, to read the cookie value of the variable named userid, the Code is as follows:
Var uId = $. cookie ('userid ');

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.