JS set of cookie-related operational functions

Source: Internet
Author: User


Detect if the browser supports cookies
function Checkcookie () {
To determine whether a cookie is turned on
var cookieenabled= (navigator.cookieenabled)? True:false;
If the browser is not ie4+ or ns6+
if (typeof navigator.cookieenabled== "undefined" &&!cookieenabled) {
Document.cookie= "TestCookie";
Cookieenabled= (document.cookie== "TestCookie")? True:falsedocument.cookie= "";
}

If it's not turned on
if (cookieenabled) {
return true;
}else{
return false;
}
}
Add cookies
function Addcookie (name,value,expirehours) {
var cookiestring=name+ "=" +escape (value);
Determine whether to set an expiration time
if (expirehours>0) {
var date=new date ();
Date.settime (date.gettime+expirehours*3600*1000);
Cookiestring=cookiestring+ "; Expire= "+date.togmtstring ();
}
document.cookie=cookiestring;
}
Get Cookie Value
function GetCookie (name) {
var Strcookie=document.cookie;
var arrcookie=strcookie.split (";");
for (Var i=0;i<arrcookie.length;i++) {
var arr=arrcookie[i].split ("=");
if (arr[0]==name) {
Return unescape (arr[1]);
}
}
return false;
}
Delete Cookies
function Deletecookie (name) {
var date=new date ();
Date.settime (Date.gettime ()-10000);
Document.cookie=name+ "=; Expire= "+date.togmtstring ();
}

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.