JavaScript manipulation Cookie class instance _javascript tips

Source: Internet
Author: User
Tags setcookie

This example describes the JavaScript manipulation cookie class. Share to everyone for your reference. Specifically as follows:

Usage:

One, set cookies

var cookie = new Jscookie ();
Normal settings
cookie. Setcookie ("Key1", "Val1");
The expiration time is one year
var expire_time = new Date ();
Expire_time.setfullyear (Expire_time.getfullyear () + 1);
Cookies. Setcookie ("Key2", "Val2", expire_time);
Sets the domain and path with the expiration Time
cookie. Setcookie ("Key3", "Val3", Expire_time, ". cnblogs.com", "/");
Set the cookie for the Ribbon key, which is the K1,K2,K3 cookie, respectively
. Setcookie ("Key4", "k1=1&k2=2&k3=3");

Second, read cookies

Easy to get
cookies. GetCookie ("Key1");
Cookies. GetCookie ("Key2");
Cookies. GetCookie ("Key3");
Cookies. GetCookie ("Key4");
Gets the subkey K1 value cookie for the Key4
. Getchild ("Key4", "K1");

Third, delete

Cookies. Expire ("Key1");
Cookies. Expire ("Key2");
Cookies. Expire ("Key3");
Cookies. Expire ("Key4");

Example:

<script type= "Text/javascript" > String.prototype.Trim = function () {return this.replace (/^\s+/g, ""). Replace (/
 \s+$/g, ""); function Jscookie () {this.
     GetCookie = function (key) {var cookie = Document.cookie;
     var cookiearray = Cookie.split (';');
     var getvalue = ""; for (var i = 0;i<cookiearray.length;i++) {if (Cookiearray[i]. Trim (). substr (0,key.length) = = key) {GetValue = Cookiearray[i].
         Trim (). substr (key.length + 1);
       Break
   } return GetValue;
   }; This. Getchild = function (Cookiekey,childkey) {var child = this.
     GetCookie (Cookiekey);
     var childs = child.split (' & ');
     var getvalue = ""; for (var i = 0;i < childs.length;i++) {if (childs[i). Trim (). substr (0,childkey.length) = = Childkey) {GetValue = Childs[i].
         Trim (). substr (childkey.length + 1);
       Break
   } return GetValue;
   }; This. Setcookie = function (key,vAlue,expire,domain,path) {var cookie = "";
     if (key!= null && value!= null) Cookie + = key + "=" + Value + ";";
     if (expire!= null) cookie + "expires=" + expire.togmtstring () + ";";
     if (domain!= null) cookie + + "domain=" + domain + ";";
     if (path!= null) cookie + = "path=" + path + ";";
   Document.cookie = cookie;
   }; This.
     Expire = function (key) {expire_time = new Date ();
     Expire_time.setfullyear (Expire_time.getfullyear ()-1);
     var cookie = "" + key + "=e;expires=" + Expire_time + ";"
   Document.cookie = cookie; }} </script>

This concludes.

I hope this article will help you with your JavaScript programming.

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.