• 【转载】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为‘/’

  • 相关阅读:
    Redis下载地址
    form序列化
    隐藏GET请求URL参数
    IntelliJ IDEA中显示方法说明快键键
    Myeclipse2014在线安装SVN插件
    Spring Cloud Gateway 集成 Sentinel 网关限流(2)
    Spring Cloud Gateway 集成 Sentinel 网关限流(1)
    Spring Cloud Gateway 集成 Nacos 实现请求负载
    微服务网关之Spring Cloud Gateway
    Nacos 的安装
  • 原文地址:https://www.cnblogs.com/zhxhdean/p/2242475.html
Copyright © 2020-2023  润新知