JavaScript设置、获取、清除单值和多值cookie的方法
更新时间:2015年11月17日 10:49:34 投稿:mrr
我要评论

cookie 是存储于访问者的计算机中的变量。每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cookie。你可以使用 JavaScript 来创建和取回 cookie 的值,本文通过一段代码给大家介绍js设置、获取、清除单值和多值cookie的方法,需要的朋友一起学习吧
废话不多说了,直接给大家贴代码了。
具体代码如下:
var CookieUtil = (function () { var Cookie = function () { // 获取单值cookie this.get = function(name) { var start = document.cookie.indexOf(encodeURIComponent(name)) ; var end = document.cookie.indexOf(';', start) ; if(end == -) { end = document.cookie.length; } return decodeURIComponent(document.cookie.substring(start+name.length+,end)); }; // 设置单值cookie this.set = function(name, value, expires, path, domain, secure) { var cookieText = encodeURIComponent(name) + "=" + encodeURIComponent(value); // 设置默认过期时间为七天 if(expires == undefined) { var date = new Date(); date.setTime(date.getTime() + ****); expires = date ; } if(expires instanceof Date) { cookieText += "; expires=" + expires.toGMTString(); } if(path != undefined) { cookieText += "; path=""; domain" + domain; } if(secure != undefined) { cookieText += "; secure"; } document.cookie = cookieText; }; // 清除单值cookie this.unset = function(name, path, domain, secure) { this.set(name, '', new Date(), path, domain, secure ); }; // 设置多值cookie this.setAll = function(name, subCookies, expires, path, domain, secure) { var cookieText = ";" + encodeURIComponent(name) + "=", arr = new Array(); for(var attr in subCookies) { arr.push([encodeURIComponent(attr)] + ":""art_xg">您可能感兴趣的文章:
相关文章
JavaScript中为什么null==0为false而null大于=0为true(个人研究)
今天闲来没啥事,研究了一下有关“null”和“0”的关系。希望大家看完了能有所收获,在此与大家分享下,希望也可以受益匪浅2013-09-09微信小程序 组件的外部样式externalClasses使用详解
这篇文章主要介绍了微信小程序里 组件的外部样式externalClasses使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2019-09-09完美解决mui框架off-canvas侧滑超出部分隐藏无法滚动的问题
下面小编就为大家分享一篇完美解决mui框架off-canvas侧滑超出部分隐藏无法滚动的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2018-01-01
最新评论