jquery操作cookie

所属分类: 网页制作 / 应用技巧 阅读数: 1488
收藏 0 赞 0 分享

复制代码
代码如下:
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});
更多精彩内容其他人还在看

出色的美工/网页设计人员需要掌握的7项技能

在互联网这行也有几年经验了,作为程序开发,经常和前端开发打交道,从和他们合作的过程中,我对美工或者网页设计有些认识,如何成为一个出色的,出类拔萃的前端开发,我觉得应该要做到下面7点
收藏 0 赞 0 分享

网页设计师需要的知识体系有哪些

设计师需要哪些体系呢
收藏 0 赞 0 分享

从四个方面谈谈Web标准的价值所在 附思维导图

清清楚楚的明白Web标准的价值!Web标准的价值相信没人会不知道,不理睬。可是具体能做什么,能不能很好的作为一个规范或者指标去诊断网站存在的问题,能不能列出一张清单,清清楚楚地看到它的价值呢
收藏 0 赞 0 分享

为什么要少用Iframe的几个原因分析

iframes 提供了一个简单的方式把一个网站的内容嵌入到另一个网站中。但我们需要慎重的使用iframe。iframe的创建比其它包括scripts和css的 DOM 元素的创建慢了 1-2 个数量级
收藏 0 赞 0 分享

百度网盟环境下的广告投放技巧(图文教程)

百度的广告投放是有技巧的.我们来谈一下吧
收藏 0 赞 0 分享

10件优秀Web开发者提升开发能力必知的事

开发工作不仅仅只是写代码,这句话来自3EV网站的Dan Frost,他在一篇文章中阐述了开发过程中应该注意的一些事项
收藏 0 赞 0 分享

不是中国才有的特色:文化差异下的网页开发

这是一篇老外写的博客,详述了他眼中因文化差异导致的网页开发问题。“特色”并不是只有中国才有,或许作为中国的设计师,也需要考虑到印尼特色、新加坡特色……
收藏 0 赞 0 分享

负距离(换位思考)-相互影响的迭代过程

产品经理把握功能 功能-指事物或方法所发挥的有利作用 .比如实用性,操作性
收藏 0 赞 0 分享

谈谈设计中的用户体验背后的8个用户本能

用户体验背后有哪些用户本能呢
收藏 0 赞 0 分享

交互设计中关于是选择分页还是加载的问题讲解

一篇文章是分页还是全捕出来是一个问题
收藏 0 赞 0 分享
查看更多