This article describes how to set, obtain, and delete cookies in javascript. It involves common javascript Cookie operations and is very simple and practical, for more information about how to set, retrieve, and delete cookies in javascript, see the following example. Share it with you for your reference. The specific implementation method is as follows:
/** Set Cookie ** name: Key of the cookie * value: value of the cookie * expires: Effective time of the cookie * path: specify the path * domain: Specify the host name * secure: Security */function setCookie (name, value, expires, path, domain, secure) {// cookie key value pair var str = name + "=" + escape (value); // set the cookie validity period, in the unit of hour if (expires> 0) {var date = new Date (); var MS = expires * 3600*1000; date. setTime (date. getTime () + MS); str + = "; expires =" + Date. toGMTString ();} if (path) {str + = "; path =" + path;} if (domain) {str + = "; domain =" + domain ;} if (secure) {str + = "; secure";} document. cookie = str;}/** obtain Cookie ** cookie_name: cookie key */function getCookie (cookie_name) {var value = null; var allcookies = document. cookie; var cookie_pos = allcookies. indexOf (cookie_name); // If an index is found, the cookie exists. // otherwise, the cookie does not exist. If (cookie_pos! =-1) {// place cookie_pos at the beginning of the value. You only need to add 1 to the value. Cookie_pos + = cookie_name.length + 1; var cookie_end = allcookies. indexOf (";", cookie_pos); if (cookie_end =-1) {cookie_end = allcookies. length;} value = unescape (allcookies. substring (cookie_pos, cookie_end);} return value;}/** Delete Cookie ** cookie_name: cookie key */function delCookie (cookie_name) {var exp = new Date (); exp. setTime (exp. getTime ()-1); var value = getCookie (cookie_name); if (value) {document. cookie = cookie_name + "=" + value + "; expires =" + exp. toGMTString ();}}
I hope this article will help you design javascript programs.