下面是人家正规网站做的 有大用
var inplay = getCookie ("inplayer" );
$ (".leftmoveLR" ).slider({range : "min" ,value : d [10 ].LleftandRight ,min : 0 ,step : 1 ,max : 100 ,slide : function (event, ui) {$ (".leftcustomize" ).css ({ left : ui.value + "%" });d [10 ].LleftandRight = ui.value ;setCookie ("product" , JSON .stringify (d ), "d30" ); } });
// JavaScript Document //-------------------------------------------------------------------------------------------------------------------------cookie //设置Cookies function setCookie (name, value, time) {var strsec = getsec (time);var exp = new Date();exp .setTime (exp .getTime () + strsec * 1 );document .cookie = name + "=" + value + ";expires=" + exp .toGMTString()+";path=/" ; }//计算时间 function getsec (str) {var str1 = str.substring (1 , str.length ) * 1 ;var str2 = str.substring (0 , 1 );if (str2 == "s" ) {return str1 * 1000 ; }else if (str2 == "h" ) {return str1 * 60 * 60 * 1000 ; }else if (str2 == "d" ) {return str1 * 24 * 60 * 60 * 1000 ; } }//这是有设定过期时间的使用示例: //s20是代表20秒 //h是指小时,如12小时则是:h12 //d是天数,30天则:d30 //读取cookies function getCookie (name) {var arr , reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)" );if (arr = document .cookie .match (reg ))return (arr [2 ]);else return null ; }//删除cookies 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()+";path=/" ; }//------------------------------------------------------------------------------------------------------------------------cookie function layermsg (msg) { layer .msg (msg, { time : 0 }); }function closemsg () { layer .closeAll (); }getCookie ("Name" );
<!-- function showHide(objname) { //只对主菜单设置cookie var obj = document.getElementById(objname); if(objname.indexOf('_1')<0 || objname.indexOf('_10')>0) { if(obj.style.display == 'block' || obj.style.display =='') obj.style.display = 'none'; else obj.style.display = 'block'; return true; } //正常设置cookie var ckstr = getCookie('menuitems'); var ckstrs = null; var okstr =''; var ischange = false; if(ckstr==null) ckstr = ''; ckstrs = ckstr.split(','); objname = objname.replace('items',''); if(obj.style.display == 'block' || obj.style.display =='') { obj.style.display = 'none'; for(var i=0; i < ckstrs.length; i++) { if(ckstrs[i]=='') continue; if(ckstrs[i]==objname){ ischange = true; } else okstr += (okstr=='' ? ckstrs[i] : ','+ckstrs[i] ); } if(ischange) setCookie('menuitems',okstr,7); } else { obj.style.display = 'block'; ischange = true; for(var i=0; i < ckstrs.length; i++) { if(ckstrs[i]==objname) { ischange = false; break; } } if(ischange) { ckstr = (ckstr==null ? objname : ckstr+','+objname); setCookie('menuitems',ckstr,7); } } }//读取cookie函数 正确 function getCookie(c_name) { 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 null; }------------------------------------------------------------------------------------------------------------------------------------------- //设置COOKIE 正确 //如果要使cookie在整个网站下可用,可以将cookieDir指定为根目录,示例如下: //document.cookie="userId=320; path=/"; //getDate()是天的意思 是当前月份的当前几号 function setCookie(c_name,value,expiredays){ var exdate=new Date(); exdate.setDate(exdate.getDate()+expiredays); document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : "; expires="+exdate.toGMTString()); }if (getCookie('url') =='') { //下面的参数 10 表示10天.因为 exdate.setDate (exdate.getDate() +expiredays); setDate 是以天为单位 getDate 也是以天为单位 setCookie('url', url, 10 ); } ------------------------------------------------------------------------------------------------------------------------------------------- //检查以前用户展开的菜单项 var totalitem = 12; function CheckOpenMenu() { //setCookie('menuitems',''); var ckstr = getCookie('menuitems'); var curitem = ''; var curobj = null; //cross_obj = document.getElementById("staticbuttons"); //setInterval("initializeIT()",20); if(ckstr==null) { ckstr='1_1,2_1,3_1'; setCookie('menuitems',ckstr,7); } ckstr = ','+ckstr+','; for(i=0;i<totalitem;i++) { curitem = i+'_'+curopenItem; curobj = document.getElementById('items'+curitem); if(ckstr.indexOf(curitem) > 0 && curobj != null) { curobj.style.display = 'block'; } else { if(curobj != null) curobj.style.display = 'none'; } } } var curitem = 1; function ShowMainMenu(n) { document.getElementById('cnssUnamePwd').style.display='none'; var curLink = $DE('link'+curitem); var targetLink = $DE('link'+n); var curCt = $DE('ct'+curitem); var targetCt = $DE('ct'+n); if(curitem==n) return false; if(targetCt.innerHTML!='') { curCt.style.display = 'none'; targetCt.style.display = 'block'; curLink.className = 'mm'; targetLink.className = 'mmac'; curitem = n; } else { var myajax = new DedeAjax(targetCt); myajax.SendGet2("index_menu_load.php?openitem="+n); if(targetCt.innerHTML!='') { curCt.style.display = 'none'; targetCt.style.display = 'block'; curLink.className = 'mm'; targetLink.className = 'mmac'; curitem = n; } DedeXHTTP = null; } } --> function getCookie(name)//取cookies函数 { var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)")); if(arr != null) return unescape(arr[2]); return ''; } url=document.referrer; if (getCookie('url') =='') { setCookie('url', url, 10); } cookie 有失效日期--expires,如果还没有过失效期,即使重新启动电脑,cookie 仍然不会丢失, 如果没有指定 expires 值,那么在关闭浏览器时,cookie 即失效。 function delCookie(name)//删除cookie { var exp = new Date(); exp.setTime(exp.getTime() - 1); var cval=getCookie(name); if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString(); } .删除指定名称的cookie:deleteCookie(name) 该函数可以删除指定名称的cookie,其实现如下: //这个是好的 <script language="JavaScript" type="text/javascript"> <!-- function deleteCookie(name){ var date=new Date(); date.setTime(date.getTime()-10000); document.cookie=name+"=''; expire="+date.toGMTString(); } //--> </script> <script> url=document.referrer; //返回上一个页面 setCookie('url', url, 10); //设置COOKIE function setCookie(c_name,value,expiredays){ var exdate=new Date(); exdate.setDate(exdate.getDate()+expiredays); document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : "; expires="+exdate.toGMTString()); } </script> //如果不设expires 那么就是会话cookie function getCookie(name)//取cookies函数 { var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)")); if(arr != null) return unescape(arr[2]); return ''; } // 这里就可以根据 名(键)值对来取cookie了 function setCookie(c_name,value,expiredays) { var exdate = new Date(); exdate.setDate(exdate.getDate() + expiredays); document.cookie = c_name + "=" +escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()); //使设置的有效时间正确。增加toGMTString() } //下面是从一个网站上拿下来的setCookie getCookie 的函数setCookie('is_pur',is_pur,30 * 24 * 60 * 60 * 1000);//因为下面 exp.setTime( exp.getTime() + t);是毫秒数 所以这里第 三个参数也是毫秒数 var cval=getCookie(name); //getTime() 方法可返回距 1970 年 1 月 1 日之间的毫秒数。
function setCookie(name,value,t) { var exp = new Date(); exp.setTime(exp.getTime() + t); document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString()+";"; } function getCookie(name) { var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)"); if(arr=document.cookie.match(reg)) return (arr[2]); else return null; } function delCookie(name) { var exp = new Date(); exp.setTime(exp.getTime() - 10000); var cval=getCookie(name); if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString()+";"; }
dedecms里面的设置cookie 获取cookie //读写cookie函数 function GetCookie(c_name) { 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 null } function SetCookie(c_name,value,expiredays) { var exdate = new Date(); exdate.setDate(exdate.getDate() + expiredays); document.cookie = c_name + "=" +escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()); //使设置的有效时间正确。增加toGMTString() }//---------------- //read COOKIE function getCookie(cookiename) { var result; var mycookie = document.cookie; var start2 = mycookie.indexOf(cookiename + "="); if (start2 > -1) { start = mycookie.indexOf("=", start2) + 1; var end = mycookie.indexOf(";", start); if (end == -1) { end = mycookie.length; } result = unescape(mycookie.substring(start, end)); } return result; } //---------------- //write COOKIE 这里 date.getTime()是毫秒数 所有增加一天就是 + Number(hours) * 3600 * 1000 function setCookie(cookiename, cookievalue, hours) { var date = new Date(); date.setTime( date.getTime() + Number(hours) * 3600 * 1000); document.cookie = cookiename + "=" + cookievalue + "; path=/;expires = " + date.toGMTString(); } 来自 http://blog.csdn.net/qingyunluck/article/details/6218675
本文讲述基本js操作cookie,同时解决两个问题:1.一个页面设置后,其他页面取不到问题。2.json数据存储失败的问题。认真读一下, 后面会解决这两个问题,如果你的cookie设置没成功。注意细节。
不多说上代码:
function getCookie (c_name ) {
if (document .cookie.length > 0 ) {
c_start = document .cookie.indexOf(c_name + "=" )
c_start = c_start + c_name.length + 1
c_end = document .cookie.indexOf(";" , c_start)
c_end = document .cookie.length
return unescape (document .cookie.substring(c_start, c_end))
function setCookie (name, value ) {
exp.setTime(exp.getTime() + 3 * 24 * 60 * 60 * 1000 );
document .cookie = name + "=" + encodeURIComponent (value)
+ ";expires=" + exp.toGMTString() + ";path=/" ;
以上代码,注意 pah=/ .此处可以解决其他页面取不到本页面设置的cookie问题,理论上同一域名下,cookie是共享,但是默认设置的时候,会加上文件夹。就会出现取不到。
window .onload = function ( ) {
$.post("queryUserInfo" , {}, function (data ) {
setCookie("userInfo" , JSON .stringify(data));
alert(getCookie("userInfo" ));
window .location = "/GovPlatform/static/view/gov/home.html" ;
取出代码:
var userInfo = JSON .parse(getCookie("userInfo" ));
以上代码,是对后台的用户信息,进行获取,然后设置到cookie中,在跳转到一个页面。那个页面自然用上面的getCookie 进行取出。
这里需要注意一个要点,这个data的json是一个对象。 会有“,”。这个“,”是特殊字符,导致不能设置到cookie中,
如果后台是java,会提示这个“that contained an invalid cookie. That cookie will be ignored.Note: further occurrences of this error will be logged at DEBUG level.”
两种解决办法, 一种是将“,”替换掉(replace)。
另一种,使用JSON.stringify(data)就可以了。取出来的时候注意使用JSON.parse(data) 如上图代码。(推荐这种)
来自 https://blog.csdn.net/u011377803/article/details/77622938
问题对人有帮助,内容完整,我也想知道答案 0 问题没有实际价值,缺少关键内容,没有改进余地
最开始使用www.test.com 设置cookie的domain:
document .cookie = "example=1; expires=Mon, 11 Nov 2026 07:34:46 GMT; domain=www.test.com;path=/"
现在更改js将cookie的domain设置为test.com:
document .cookie = "example=2; expires=Mon, 11 Nov 2026 07:34:46 GMT; domain=test.com;path=/"
但是此时在页面中调取document.cookie时会出现两个example值:
"example=1; example=2"
问题: 1.请问如何取到使用新的domain设置的cookie值? 2.请问如何删除原来的cookie?
ps: 1.所有的页面都是用这一个js。 2.原来设置的cookie的域是带子域的,如a.test.com、b.test.com,且子域的数量比较多。所以个人认为通过遍历为对应要删除的cookie设置过期时间是不效率的的。 3.不知道是否有办法判断出cookie对应的domain? 4.请尽量给出前端的解决办法
来自 https://segmentfault.com/q/1010000007676568