Functions related to cookie operations in javascript

Source: Internet
Author: User

The operation of cookie cannot be performed on the cookie, that is, the read/write deletion operation. I will give you three examples to introduce the method of using js to operate the cookie. For more information, see.

Instance
// Set the cookie function. The first parameter is the cookie name, the second parameter is the value, and the third parameter is the cookie retention time (unit: Day)

The Code is as follows: Copy code
Function setCookie (name, value, days ){
Var days = arguments [2]? Arguments [2]: 30; // if there are no days, this cookie is saved for 30 days by default.
Var exp = new Date ();
Exp. setTime (exp. getTime () + days * 86400000 );
Document. cookie = name + "=" + escape (value) + "; expires =" + exp. toGMTString ();
}

// Read the cookies Function

The Code is as follows: Copy code
Function getCookie (name ){
Var arr = document. cookie. match (new RegExp ("(^ |)" + name + "= ([^;] *) (; | $ )"));
If (arr! = Null ){
Return unescape (arr [2]);
}
Return null;
}

// Delete the cookie Function

The Code is as follows: Copy code
Function delCookie (name ){
Var exp = new Date ();
Exp. setTime (exp. getTime ()-1 );
Var cval = getCookie (name );
If (cval! = Null ){
Document. cookie = name + "=" + cval + "; expires =" + exp. toGMTString ();
}
}


Encapsulated cookies:

The Code is as follows: Copy code

Var cookie = new function (){
This. set = function (name, value, hours ){
Var life = new Date (). getTime ();
Life + = hours * 1000*60*60;
Var cookieStr = name + "=" + escape (value) + "; expires =" + new Date (life). toGMTString () + "; path = /";
Document. cookie = cookieStr;
};
This. get = function (name ){
Var cookies = document. cookie. split (";");
Var I = 0;
For (I = 0; I <cookies. length; I ++ ){
Var cookie2 = cookies [I]. split ("= ");
If (cookie2 [0] = name) {return unescape (cookie2 [1]);}
}
Return '';
};
This. remove = function (name ){
Var cookieStr = name + "='' "+ escape ('null') +"; expires = "+ new Date (). toGMTString ();
Document. cookie = cookieStr;
};
}

Related Article

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.