两种cookie操作方式代码
来源:互联网 发布:联通网络ip地址丢失 编辑:程序博客网 时间:2024/06/05 20:07
两种cookie操作方式代码:
1、js原生
var G = G || {}; G.Cookie = { Get: function (name) { var cv = document.cookie.split("; "); var cva = [], temp; for (i = 0; i < cv.length; i++) { temp = cv[i].split("="); cva[temp[0]] = unescape(temp[1]); } if (name) return cva[name]; else return cva; }, Set: function (name, value, expires, path, domain, secure) { if (!name || !value) return false; if (name == "" || value == "") return false; if (expires) { if (/^[0-9]+$/.test(expires)) { var today = new Date(); expires = new Date(today.getTime() + expires * 1000).toGMTString(); } else if (!/^wed, d...{2} w...{3} d...{4} d...{2}:d...{2}:d...{2} GMT$/.test(expires)) { expires = undefined; } } var cv = name + "=" + escape(value) + ";" + ((expires) ? " expires=" + expires + ";" : "") + ((path) ? "path=" + path + ";" : "") + ((domain) ? "domain=" + domain + ";" : "") + ((secure && secure != 0) ? "secure" : ""); if (cv.length < 4096) { document.cookie = cv; return true; } else { return false; } }, Del: function (name, path, domain) { if (!name) return false; if (name == "") return false; if (!this.Get(name)) return false; document.cookie = name + "=;" + ((path) ? "path=" + path + ";" : "") + ((domain) ? "domain=" + domain + ";" : "") + "expires=Thu, 01-Jan-1970 00:00:01 GMT;"; return true; } }
2、基于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; } };
0 0
- 两种cookie操作方式代码
- Extjs cookie操作的两种方式
- ExtJs设置cookie两种方式
- HttpClient获取Cookie的两种方式
- 浏览器获取cookie的两种方式
- COOKIE的几种操作方式
- java获取页面cookie的两种方式
- Fiddler和wireshark两种工具抓cookie方式
- Android 两种异步操作方式
- php mysqli两种操作方式示例
- C#文件操作的两种方式
- 指针操作数组的两种方式
- nodejs操作MSSQL两种方式--笔记
- MyBatis操作数据库的两种方式
- 服务器端代码单元测试两种方式
- git提交代码的两种方式
- 代码合并的两种方式详解
- Python 执行代码的两种方式
- (初识)UrlRewriteFilter的使用
- 【HDU 5312】Sequence(数学问题)
- XMPP查找联系人出错
- 设置滚动效果
- Hama查询zookeerp节点
- 两种cookie操作方式代码
- Android View.onMeasure方法的理解
- base64的js实现
- java性能调优(转载)
- Android Fragment+ViewPager实现循环滑动
- 【DP】 BZOJ 1487: [HNOI2009]无归岛
- 几个不常用但是很好用的PHP数组函数
- fragment的一些探讨
- c语言优化技巧