JavaScript设置、获取、清除单值和多值cookie的方法_javascript技巧
程序员文章站
2022-05-07 08:46:45
...
废话不多说了,直接给大家贴代码了。
具体代码如下:
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=" + path; } if(domain != undefined) { cookieText += "; 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)] + ":" + encodeURIComponent(subCookies[attr])); } this.set(name, arr.join('&'), expires, path, domain, secure); }; // 获取多值cookie this.getAll = function(name) { var obj = {}; var arr = this.get(name).split('&'); for(var i = , len = arr.length; i
以上代码就是本文给大家介绍JavaScript设置、获取、清除单值和多值cookie的方法,有哪里不清楚的地方欢迎给我留言。
上一篇: CSS文本方向 - 小火柴的蓝色理想
推荐阅读
-
JavaScript获取和设置CheckBox状态的简单方法_javascript技巧
-
JavaScript获取和设置CheckBox状态的简单方法_javascript技巧
-
JS获取下拉框显示值和判断单选按钮的方法_javascript技巧
-
js 通过html()及text()方法获取并设置p标签的显示值_javascript技巧
-
Js与Jq获取浏览器和对象值的方法_javascript技巧
-
js 获取和设置css3 属性值的实现方法_javascript技巧
-
js 通过html()及text()方法获取并设置p标签的显示值_javascript技巧
-
javascript实现设置、获取和删除Cookie的方法_javascript技巧
-
JavaScript设置、获取、清除单值和多值cookie的方法_javascript技巧
-
js 获取和设置css3 属性值的实现方法_javascript技巧