varCookie =function(name, value, options) {if(typeofValue!== ' undefined ') {Options= Options | | {}; if(Value = = =NULL) {Value= ' '; Options=$.extend ({}, Options); Options.expires=-1; } varexpires = ' '; if(Options.expires && (typeofOptions.expires = = = ' Number ' | |options.expires.toUTCString)) {vardate; if(typeofOptions.expires = = = ' Number ') {Date=NewDate (); Date.settime (Date.gettime ()+ (Options.expires * 24 * 60 * 60 * 1000)); } Else{Date=Options.expires; } Expires= '; Expires= ' +date.toutcstring (); } varPath = Options.path? ‘; Path= ' + (Options.path): ';p ath=/'; varDomain = Options.domain? ‘; Domain= ' + (options.domain): '; varSecure = options.secure? ‘; Secure ': '; Document.cookie= [Name, ' = ', encodeURIComponent (value), expires, path, domain, Secure].join ("); } Else { varCookievalue =NULL; if(Document.cookie && document.cookie!== ") { varcookies = Document.cookie.split ('; ')); for(vari = 0; i < cookies.length; i++) { varCookie =$.trim (Cookies[i]); if(cookie.substring (0, name.length + 1) = = = (name + ' = ')) ) {Cookievalue= decodeURIComponent (cookie.substring (name.length + 1)); Break; } } } returnCookievalue; } } varRemovecookie =function(key) {$.cookie (key,‘‘, {expires:-1 }); }
varStorage =function(St, key, value, expires) {if(st = = ' l ')) {St=Window.localstorage; Expires= Expires | | 60; } Else{St=Window.sessionstorage; Expires= Expires | | 5; } if(typeofValue! = ' undefined ') { Try { returnSt.setitem (Key, Json.stringify ({data:value, Expires:NewDate (). GetTime () + expires * 1000 * 60 })); } Catch(e) {}}Else { varresult = Json.parse (St.getitem (key) | | ‘{}‘); if(Result &&NewDate (). GetTime () <result.expires) {returnResult.data; } Else{St.removeitem (key); return NULL; } } }
Encapsulation Cookie Localstorage Sessionstorage