1 function Cookie() {}; 2 Cookie.prototype = { 3 constructor: Cookie, 4 _path: "/", 5 expire: new Date(), 6 setPath: function(path) { 7 this._path = !path ? this._path : path; 8 }, 9 getPath: function() { 10 return this._path; 11 }, 12 set: function(name, value, day) { 13 if (this.get(name)) this.del(name); 14 day = day ? day : 0; 15 this.expire.setTime(this.expire.getTime() + day * 24 * 3600 * 1000); 16 document.cookie = name + "=" + escape(value) + (day != 0 ? ";expires=" + this.expire.toGMTString() : "") + ";path=" + this.getPath(); 17 }, 18 get: function(name) { 19 var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)")); 20 if (arr != null) { 21 return unescape(arr[2]); 22 } 23 }, 24 del: function(name) { 25 if (this.get(name)) { 26 document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT;path=" + this.getPath(); 27 } 28 }, 29 delAll: function() { 30 var arr = document.cookie.match(/[^ =;]+(?==)/g); 31 if (arr) { 32 for (var i = arr.length; i--;) { 33 this.del(arr[i]); 34 } 35 } 36 } 37 }; 38 var cookie = new Cookie(); 39 cookie.set('flag', 'audit');