JS JavaScript operation cookie

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.