Home >Web Front-end >JS Tutorial >Detailed explanation of cookie operations in JS
name Cookie name, key value
value Optional, cookie value
expire Optional, expiration time, timestamp format
path Optional, server-side valid path, / means the entire domain name is valid, the default is the path of the page when the cookie is currently set
domain Optional, the domain name that this cookie is valid for
secure Optional. Specifies whether cookies are transmitted over a secure HTTPS connection.
(function(){ var cookieObj={ 'add':function(name, value, hours){ //修改或是添加cookie var expire = ""; if(hours != null){ expire = new Date((new Date()).getTime() + hours * 3600000); expire = "; expires=" + expire.toGMTString(); } document.cookie = name + "=" + escape(value) + expire + ";path=/"; //如果指定域名可以使用如下 //document.cookie = name + "=" + escape(value) + expire + ";path=/;domain=findme.wang"; }, 'get':function(c_name){//读取cookie if (document.cookie.length>0){ c_start=document.cookie.indexOf(c_name + "=") if (c_start!=-1){ c_start=c_start + c_name.length+1 c_end=document.cookie.indexOf(";",c_start) if (c_end==-1){ c_end=document.cookie.length } return unescape(document.cookie.substring(c_start,c_end)) } } return ""; } }; window.cookieObj=cookieObj;}());
cookieObj.add('myWeb','http://www.findme.wang'); console.log('myWeb:'+cookieObj.get('website'));