1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| cookie helper class easy to write,get,delete */ var myCookie={ get:function(name){ if(typeof name != "undefined") { return myCookie_get(name); }else{ return myCookie_getAll(); } }, add:function(name,value,options){ myCookie_add(name,value,options); }, delete:function(name){ myCookie_add(name,null); } }
String.prototype.Trim = function() { return this.replace(/^\s+/g,"").replace(/\s+$/g,""); }
cookie write function @name:the cookie name not null @value:the cookie value null==delete the cookie @option:{"expires":expire time;"path":/;"domain":localhost;"secure":secure} */ function myCookie_add(name,value,options) { if (typeof value != 'undefined') { 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(); } 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(''); } }
get the name cookie @name:the cookie's name */ function myCookie_get(name) { var cookieValue = null; if (document.cookie && document.cookie != '') { var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = cookies[i].Trim(); if (cookie.substring(0, name.length + 1) == (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; }
get all the cookie return as a json */ function myCookie_getAll() { var cookieArray = new Array(); var str=""; var temp; if (document.cookie && document.cookie != '') { var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = cookies[i].Trim(); temp=cookie.split('='); cookieArray.push("{\"name\":\""+decodeURIComponent(temp[0])+"\",\"value\":\""+decodeURIComponent(temp[1])+"\"}"); } str=cookieArray.join(","); } str="["+str+"]"; return eval('('+str+')'); }
|