前段时间在一个项目中涉及到cookie的存取,于是打算封装一个 cookie 的CRUD 。按理来说,这本身是一个很简单的问题,不注意的话简单的问题也有大坑。 1 /** 2 * Set or get cookie ;parse to object 3 * 4 *@date ...
前段时间在一个项目中涉及到cookie的存取,于是打算封装一个 cookie 的CRUD 。按理来说,这本身是一个很简单的问题,不注意的话简单的问题也有大坑。
1 /** 2 * Set or get cookie ;parse to object 3 * 4 *@date 2015-04-11 5 * 6 *@author 飘摇的枫叶 7 * 8 * 9 * 10 */ 11 'use strict'; 12 (function (window) { 13 // class2type refer to zepto.js $.type(obj) ## https://github.com/madrobby/zepto 14 var class2type = {}, 15 toString = class2type.toString; 16 "Boolean Number String Function Array Date RegExp Object Error".split(" ").forEach(function(name) { 17 class2type["[object " + name + "]"] = name.toLowerCase(); 18 }); 19 20 var _qcookie = { 21 cookies: document.cookie, 22 get: function(name) { 23 return this.all()[name]; 24 }, 25 /* 26 *Set cookie 27 * 28 *@param {string} cookieName 29 *@param {string} cookieValue 30 * or 31 *@param {object} cookieObject{key:value} 32 * 33 *@param {object} options{path:'/demo';domain:'/';expire:new Date();secure:0|1} 34 */ 35 set: function(name, value, options) { 36 var opt = options || {}; 37 if (!name) return; 38 if (type(name) === 'string' && value) { 39 this.write(name, value, opt); 40 } else if (type(name) === 'object') { 41 opt = value || {}; 42 for (var k in name) { 43 this.write(k, name[k], opt); 44 } 45 } 46 // if (type(name) === 'string' && value) { 47 // str = encode(name) + '=' + encode(value) + '; '; 48 // } else if (type(name) === 'object') { 49 // opt = value || {}; 50 // for (var k in name) { 51 // str += encode(k) + '=' + encode(name[k]) + '; '; 52 // } 53 // } 54 // if (opt.path) str += 'path=' + opt.path; 55 // if (opt.domain) str += 'domain=' + opt.domain; 56 // if (opt.expires) str += 'expires=' + opt.expires.toUTCString(); 57 // if (opt.secure) str += 'secure'; 58 // document.cookie = str; 59 }, 60 all: function() { 61 return this.parse(this.cookies); 62 }, 63 delete: function(name) { 64 var expires = new Date(); 65 expires.setDate(expires.getDate() - 1); 66 document.cookie = name + "=" + this.get(name) + '; expires=' + expires.toUTCString(); 67 }, 68 write: function(name, value, opt) { 69 var str = encode(name) + '=' + encode(value); 70 if (opt.path) str += '; path=' + opt.path; 71 if (opt.domain) str += '; domain=' + opt.domain; 72 if (opt.expires) str += '; expires=' + opt.expires.toUTCString(); 73 if (opt.secure) str += '; secure'; 74 document.cookie = str; 75 }, 76 parse: function(cookieStr) { 77 var obj = {}; 78 var arrs = cookieStr.split(/ *; */); // Similar with the split(';') + trim() 79 var kv = []; 80 if (arrs[0] == '') { 81 return obj; 82 } 83 for (var i = 0; i < arrs.length; i++) { 84 kv = arrs[i].split('='); 85 obj[decode(kv[0])] = decode(kv[1]); 86 } 87 return obj; 88 } 89 } 90 91 function encode(str) { 92 return encodeURIComponent(str); 93 } 94 95 function decode(str) { 96 return decodeURIComponent(str); 97 } 98 99 function type(obj) {100 return obj == null ? String(obj) :101 class2type[toString.call(obj)] || "object"102 }103 window.qcookie = _qcookie;104 })(window)
海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com
原标题:关于 cookie 使用中遇到的问题
关键词:ie
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。