zoukankan      html  css  js  c++  java
  • 【转载】jquery操作cookie

    jQuery.cookie = function(name, value, options) { 
    if (typeof value != 'undefined') { // name and value given, set cookie
    options = options || {};
    if (value === null) {
    value = '';
    options.expires = -1;
    }
    var expires = '';
    if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
    var date;
    if (typeof options.expires == 'number') {
    date = new Date();
    date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
    } else {
    date = options.expires;
    }
    expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
    }
    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 { // only name given, get cookie
    var cookieValue = null;
    if (document.cookie && document.cookie != '') {
    var cookies = document.cookie.split(';');
    for (var i = 0; i < cookies.length; i++) {
    var cookie = jQuery.trim(cookies[i]);
    // Does this cookie string begin with the name we want?
    if (cookie.substring(0, name.length + 1) == (name + '=')) {
    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
    break;
    }
    }
    }
    return cookieValue;
    }
    };

    以上代码保存为jqcookie.js,在需要操作cookie的页面引用jq及jqcookie

    新建一个cookie 包括有效期 路径 域名等 : $.cookie(’the_cookie’, ‘the_value’, {expires: 7, path: ‘/’, domain: ‘jquery.com’, secure: true});
    删除cookie :$.cookie(’the_cookie’, null);
    自己在使用中发现删除cookie的时候, $.cookie('51snk', '', { path:'/'}); 用这样的代码更加合理。因为添加cookie 的时候设置的path为‘/’

  • 相关阅读:
    第八章 线性时间排序
    第七章 快速排序
    第六章 堆排序
    第四章 分治策略
    第二章 算法基础
    第一章 算法在计算中的作用
    opencv —— cornerSubPix 亚像素级角点检测
    opencv —— Shi-Tomasi 角点检测
    .NET List<T>Conat vs AddRange
    自定义组装json对象
  • 原文地址:https://www.cnblogs.com/zhxhdean/p/2242475.html
Copyright © 2011-2022 走看看