zoukankan      html  css  js  c++  java
  • jQuery操作cookie 的方法

    转载于:http://blog.163.com/lih_dodo@126/blog/static/30551823201111411926425/

    jQuery cookie是个很好的cookie插件,大概的使用方法如下

    example $.cookie(’name’, ‘value’); 设置cookie的值,把name变量的值设为value

    example $.cookie(’name’, ‘value’, {expires: 7, path: ‘/’, domain: ‘jquery.com’, secure: true}); 新建一个cookie 包括有效期 路径 域名等

    example $.cookie(’name’, ‘value’); 新建cookie

    example $.cookie(’name’, null); 删除一个cookie

    var account= $.cookie('name'); 取一个cookie(name)值给myvar

    代码如下

     1 // JavaScript Document
     2 jQuery.cookie = function(name, value, options) {
     3     if (typeof value != 'undefined') { // name and value given, set cookie
     4         options = options || {};
     5         if (value === null) {
     6             value = '';
     7             options.expires = -1;
     8         }
     9         var expires = '';
    10         if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
    11             var date;
    12             if (typeof options.expires == 'number') {
    13                 date = new Date();
    14                // date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));//单位太大,换一个。
    15                 date.setTime(date.getTime() + (options.expires * 60 * 60 * 1000));
    16             } else {
    17                 date = options.expires;
    18             }
    19             expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
    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 { // only name given, get cookie
    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                 // Does this cookie string begin with the name we want?
    32                 if (cookie.substring(0, name.length + 1) == (name + '=')) {
    33                     cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
    34                     break;
    35                 }
    36             }
    37         }
    38         return cookieValue;
    39     }
    40 };
  • 相关阅读:
    [LeetCode 1029] Two City Scheduling
    POJ 2342 Anniversary party (树形DP入门)
    Nowcoder 106 C.Professional Manager(统计并查集的个数)
    2018 GDCPC 省赛总结
    CF 977 F. Consecutive Subsequence
    Uva 12325 Zombie's Treasure Chest (贪心,分类讨论)
    Poj 2337 Catenyms(有向图DFS求欧拉通路)
    POJ 1236 Network of Schools (强连通分量缩点求度数)
    POJ 1144 Network (求割点)
    POJ 3310 Caterpillar(图的度的判定)
  • 原文地址:https://www.cnblogs.com/zhucheer/p/2868202.html
Copyright © 2011-2022 走看看