Js cookie-related operation functions

Source: Internet
Author: User

Js cookie-related operation functions

 

// Check whether the browser supports cookies
Function checkCookie (){
// Determine whether the cookie is enabled
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 not enabled
If (cookieEnabled ){
Return true;
} Else {
Return false;
}
}
// Add Cookie
Function addCookie (name, value, expireHours ){
Var cookieString = name + "=" + escape (value );
// Determine whether to set the expiration time
If (expireHours> 0 ){
Var date = new Date ();
Date. setTime (date. getTime + expireHours * 3600*1000 );
CookieString = cookieString + "; expire =" + date. toGMTString ();
}
Document. cookie = cookieString;
}
// Obtain the 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 the Cookie
Function deleteCookie (name ){
Var date = new Date ();
Date. setTime (date. getTime ()-10000 );
Document. cookie = name + "=; expire =" + date. toGMTString ();
}

 

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.