1 varCookieutil = {2Getfunction(name) {3 varCookieName = encodeURIComponent (name) + "=",4Cookiestart =document.cookie.indexOf (cookiename),5Cookievalue =NULL;6 if(Cookiestart >-1) {7 varCookieend = Document.cookie.indexOf (";", Cookiestart);8 if(Cookieend = =-1) {9Cookieend =document.cookie.length;Ten } OneCookievalue = decodeURIComponent (document.getCookie.substring (Cookiestart +cookiename.length, Cookieend)); A } - returnCookievalue; - }, theSetfunction(name, value, expires, path, domain, secure) { - varCookietext = encodeURIComponent (name) + "=" + - encodeURIComponent (value); - if(ExpiresinstanceofDate) { +Cookietext + = "; Expires= "+expires.togmtstring (); - } + if(path) { ACookietext + = "; Path= "+path; at } - if(domain) { -Cookietext + = "; Domain= "+domain; - } - if(secure) { -Cookietext + = "; Secure; in } -Document.cookie =Cookietext; to }, +unsetfunction(name, path, domain, secure) { - This. Set (Name, "",NewDate (0), path, domain, secure); the } *};
Cookies have a high degree of extensibility and usability, but there are drawbacks: first: The total number of cookies per domain is limited, but there are differences between browsers. As shown below
- IE6 and lower versions limit up to 20 cookies per domain name.
- IE7 and later versions each domain name has a maximum of 50. IE7 initially supported a maximum of 20 cookies per domain name and was later updated by a Microsoft patch.
- Firefox limits a maximum of 50 cookies per domain.
- Opera restricts a maximum of 30 cookies per domain.
When a cookie is set beyond the limit of a single domain name, the browser clears the previously set cookie. IE and Opera will delete the least recently used (Lru,least recently used) cookie to make room for the new set of cookies. Firefox seems to randomly decide which cookie to erase, so it's important to consider cookie restrictions to avoid unintended consequences.
Second: consider security issues. If a cookie is intercepted, the interceptor can get all the session information. Even if encryption is not used, the interceptor does not need to know the meaning of the cookie, which can cause security problems.
Cookie method encapsulation and cookie disadvantage analysis