var cookieutil = {//Get cookie value by name such as Cookieutil.set ("Data", "NAME=NIC&BOOK=PAGEXOFFSET%20JS"); Alert (COOKIEUTIL.G ET ("data")); Get:function (name) {var cookiename = encodeURIComponent (name) + "=", Cookiestart = document.cookie.in Dexof (CookieName),//document.cookie's attribute cookievalue = null; if (Cookiestart >-1) {var cookieend = document.cookie.indexOf (";", Cookiestart); if (cookieend = =-1) {cookieend = Document.cookie.length; } Cookievalue = decodeURIComponent (document.cookie.substring (Cookiestart + cookiename.length, cookieend)); } return cookievalue; },//set-cookie:name=value;expires:mon,22-jan-07 07:10:24 gmt;domain=.wrox.com;path=/;secure//Set Cookie Set:func tion (name,value,expires,path,domain,secure) {var cookietext = encodeURIComponent (name) + "=" + encodeURIComponent (value); if (expires instanceof Date) {Cookietext+ = "; expires=" + expires.togmtstring (); } if (path) {Cookietext + = ";p ath=" + path; } if (domain) {cookietext + = ";d omain=" + domain; } if (secure) {//using SSL Cookietext + = "; secure"; } document.cookie = Cookietext; },//Cancel cookie (causes cookie value to expire) unset:function (name, path, domain, secure) {This.set (name, "", New Date (0), Path , domain, secure); }}//cookies use cookie values to store multiple name value pairs such as name=name1=value1&name2=value2&name3=value3//such as var data = Subcookieutil.getall ("Data");//alert (data.name);//alert (Data.book); var subcookieutil = {//Get cookie value and its sub-cookie Get:f Unction (name, subname) {var subcookies = This.getall (name); if (subcookies) return subcookies[subname]; else return null; },//Fetch all sub-cookie object Getall:function (name) {var cookiename = encodeURIComponent (name) + "=", cook Iestart = Document.cookie.indexOf (coOkiename), Cookievalue = null, Cookieend, subcookies, I, parts, result = {}; if (Cookiestart >-1) {cookieend = Document.cookie.indexOf (";", Cookiestart); if (cookieend = =-1) {cookieend = Document.cookie.length; } Cookievalue = document.cookie.substring (Cookiestart + cookiename.length, cookieend); if (Cookievalue.length > 0) {subcookies = decodeURIComponent (Cookievalue). Split ("&"); for (i = 0, len = subcookies.length; i < Len; i++) {parts = subcookies[i].split ("="); Result[decodeuricomponent (parts[0])] = decodeURIComponent (parts[1]); } return result; }} return null; },//Set sub-cookie Set:function (name,subname,value,expires,path,domain,secure) {var subcookies = This.getall (n AME) | | {}; Subcookies[subname] = value; This.setall (name,subcookies,expires,path,domain,secure); }, Setall:function (name,subcookies,expires,path,domain,secure) {var cookietext = encodeURIComponent (name) + " = ", Subcookieparts = new Array (), subname; For (SubName in subcookies) {if (subname.length > 0 && subcookies.hasownproperty (subname)) { Subcookieparts.push (encodeURIComponent (subname) + "=" + encodeURIComponent (Subcookies[subname])); }} if (Cookieparts.length > 0) {cookietext + = Subcookieparts.join ("&"); if (expires instanceof Date) {cookietext + = "; expires=" + expires.togmtstring (); } if (path) {Cookietext + = ";p ath=" + path; } if (domain) {cookietext + = ";d omain=" + domain; } if (secure) {cookietext = = "; secure"; }} else { Cookietext + = "; expires=" + (new Date (0)). togmtstring (); } document.cookie = Cookietext; },//Logoff sub-cookie unset:function (name,subname,path,domain,secure) {var subcookies = This.getall (name); if (subcookies) {delete Subcookies[subname]; This.setall (name, subcookies, NULL, PATH, domain, secure); }}, Unsetall:function (name, path, domain, secure) {this.setall (name, NULL, new Date (0), path, domain, sec URE); }}
Javascipt Native Set cookie encapsulated module, can be used directly, suitable for most browsers