日期:2014-05-16 浏览次数:20353 次
/***************************************/ /* title: operate cookie /* author: danica7773 /**************************************/ var cookieOperate = { isCookie: function() { return navigator.cookieEnabled; }, cookieMap: function() { var _map = {}; if(this.isCookie()) { var _cookie = document.cookie.split(';'); var _len = _cookie.length; var _subItem; for(var i = 0; i < _len; i++) { _subItem = _cookie[i].split('='); _map[decodeURI(_subItem[0].replace(/^\s*|\s*$/g, ''))] = decodeURI (_subItem[1]); } } return _map; }, searchCookie: function(key) { var _map = this.cookieMap(); return (_map[key])? _map[key] : null; }, /** /*op = { /* k: key, /* d: domain, /* p: path, /* } **/ deleteCookie: function(op) { var _value = this.searchCookie(op.k); if(_value) { var _op = { v: '', t: -1 } for(e in op){ _op[e] = op[e]; } this.writeCookie(_op); } }, /** /*op = { /* k: key, /* v: value, /* f: true | false, //(cover the old record) /* s: splitFlag /* n: 10, //max number /* t: expires, //seconds /* d: domain, /* p: path, /* } **/ writeCookie: function(op) { var _cookie = []; if(op.k && typeof(op.v) !== 'undefined' && op.v !== null) { var _v = encodeURI(op.v); if(!op.f) { var _s = (op.s)? op.s : '|'; var _n = (op.n)? op.n : 10; var _old = this.searchCookie(op.k); if(_old) { var _arr = _old.split(_s); var _len = _arr.length; for(var i = 0; i < _n - 1; i++) { if(!_arr[i]) { break; } _v += _s + encodeURI(_arr[i]); } } } _cookie.push(op.k + '=' + _v); } if(op.t) { _cookie.push('expires=' + this.setExpires(op.t)); } if(op.d) { _cookie.push('domain=' + op.d); } if(op.p) { _cookie.push('path=' + op.p); } document.cookie = _cookie.join(';'); }, setExpires: function(seconds) { var _d = new Date(); _d.setTime(_d.getTime() + (seconds * 1000)); return _d.toGMTString(); } }
------解决方案--------------------
不好意思,有一个方法写的有问题,改成下面的。
cookieMap: function() { var _map = {}; if(this.isCookie()) { v