String. prototype. trim = function () <br/>{< br/> return this. replace (/^/S +/g ,""). replace (// s + $/g, ""); <br/>}</P> <p> function jscookie () <br/>{< br/> This. getcookie = function (key) <br/>{< br/> var cookie = document. cookie; <br/> var cookiearray = cookie. split (';'); <br/> var getvalue = ""; <br/> for (VAR I = 0; I <cookiearray. length; I ++) <br/>{</P> <p> If (cookiearray [I]. trim (). substr (0, key. length) = Key) <br/>{< br/> getvalue = cookiearray [I]. trim (). substr (key. length + 1); <br/> break; <br/>}</P> <p> return getvalue; <br/> }; <br/> This. getchild = function (cookiekey, childkey) <br/>{< br/> var child = This. getcookie (cookiekey); <br/> var Childs = child. split ('&'); <br/> var getvalue = ""; </P> <p> for (VAR I = 0; I <Childs. length; I ++) <br/>{< br/> If (Childs [I]. trim (). substr (0, childk Ey. length) = childkey) <br/>{< br/> getvalue = Childs [I]. trim (). substr (childkey. length + 1); <br/> break; <br/>}< br/> return getvalue; <br/>}; <br/> This. setcookie = function (Key, value, expire, domain, PATH) <br/>{< br/> var cookie = ""; <br/> If (key! = NULL & value! = NULL) <br/> cookie + = Key + "=" + value + ";"; <br/> If (expire! = NULL) <br/> cookie + = "expires =" + expire. togmtstring () + ";"; <br/> If (domain! = NULL) <br/> cookie + = "Domain =" + domain + ";"; <br/> If (path! = NULL) <br/> cookie + = "Path =" + path + ";"; <br/> document. cookie = cookie; <br/>}; <br/> This. expire = function (key) <br/>{< br/> expire_time = new date (); <br/> expire_time.setfullyear (expire_time.getfullyear ()-1 ); <br/> var cookie = "" + key + "= E; expires =" + expire_time + ";" <br/> document. cookie = cookie; <br/>}</P> <p> usage: <br/> 1. Set cookie <br/> var cookie = new jscookie (); <br/> // common setting <br/> cookie. setcookie ("key1", "val1"); </P> <p> // The expiration time is one year <br/> var expire_time = new date (); <br/> expire_time.setfullyear (expire_time.getfullyear () + 1); <br/> cookie. setcookie ("key2", "val2", expire_time); </P> <p> // set the domain and path with the expiration time <br/> cookie. setcookie ("key3", "val3", expire_time, ".cnblogs.com", "/"); </P> <p> // set the cookie of the BIND key, the subkeys are K1, K2, and K3. <br/> cookie. setcookie ("key4", "k1 = 1 & k2 = 2 & K3 = 3 "); </P> <p> 2. Read the cookie <br/> // obtain the cookie. <br/>. getcookie ("key1"); <br/> cookie. getcookie ("key2"); <br/> cookie. getcookie ("key3"); <br/> cookie. getcookie ("key4"); <br/> // obtain the key 1 Value of key4 <br/> cookie. getchild ("key4", "K1"); </P> <p> 3. Delete <br/> cookies. expire ("key1"); <br/> cookie. expire ("key2"); <br/> cookie. expire ("key3"); <br/> cookie. expire ("key4"); <br/>
From: http://topic.csdn.net/u/20100827/22/2335d1ad-a335-4c5a-8391-1736c998ea46.html