博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jquery操作cookie
阅读量:7281 次
发布时间:2019-06-30

本文共 2979 字,大约阅读时间需要 9 分钟。

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;    }};function getcookie(name) {var cookie_start = document.cookie.indexOf(name);var cookie_end = document.cookie.indexOf(";", cookie_start);return cookie_start == -1 ? '' : unescape(document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length)));}function setcookie(cookieName, cookieValue, seconds, path, domain, secure) {    var expires = new Date();    expires.setTime(expires.getTime() + seconds);    document.cookie = escape(cookieName) + '=' + escape(cookieValue)                                            + (expires ? '; expires=' + expires.toGMTString() : '')                                            + (path ? '; path=' + path : '/')                                            + (domain ? '; domain=' + domain : '')                                            + (secure ? '; secure' : '');}

使用方法

提供方便方法操作cookie : 

$.cookie('the_cookie'); // 获得cookie
$.cookie('the_cookie', 'the_value'); // 设置cookie
$.cookie('the_cookie', 'the_value', { expires: 7 }); //设置带时间的cookie  7天
$.cookie('the_cookie', '', { expires: -1 }); // 删除
$.cookie('the_cookie', null); // 删除 cookie

设置cookie的名值对,有效期,路径,域,安全

$.cookie(’name’, ‘value’, {expires: 7, path: ‘/’, domain: ‘jquery.com’, secure: true});

转载于:https://www.cnblogs.com/Yellowshorts/p/3610697.html

你可能感兴趣的文章
open .ost file
查看>>
Windows Phone 7 Tips (5)
查看>>
解决C++代码单元测试中的难题-不可验证和IO调用
查看>>
对公司网站DNS解析异常的排查与处理
查看>>
visual assist 1837 支持vs2010
查看>>
android用户界面-菜单
查看>>
Mdaemon邮件服务器作为Exchange反垃圾网关部署方案
查看>>
快速更改Windows 7桌面显示
查看>>
Vlookup函数的两个使用案例分享
查看>>
常用界面布局(LinearLayout以此做的DEMO)
查看>>
【移动开发】Android中Theme和Style的使用
查看>>
TrieTree服务续篇 - 组件构成及其作用
查看>>
Linux管道命令
查看>>
MySQL 转换函数与运算符
查看>>
针对RemoteFX的Quadro
查看>>
FileItem 出现部分中文乱码解决办法
查看>>
zabbix 报警小案例
查看>>
Google Developing for Android 学习总结
查看>>
在centos7中添加一个新用户,并授权
查看>>
SWIFT中函数返回值为Tuple
查看>>