请问用javascript操作cookie失败?

请问用javascript操作cookie失败?


慕运维8079593
浏览 355回答 4
4回答

九州编程

你的setCookie方法有点问题,改成下面的试试!我这试过可以的function setCookie(name, value, expires, path, domain, secure) {var today = new Date();if (expires) {expires = expires * 1000 * 60 * 60 * 24;}var expires_date = new Date(today.getTime() + expires);try {document.cookie+=name+"="+escape(value);if(value) document.cookie+=";expires=" + expires_date.toGMTString();if(path) document.cookie+=";path=" + path;if(domain)document.cookie+";domain=" + domain;if(secure)document.cookie+";secure" + secure;} catch (e) {throw Error(e);}}就是你那((expires) 这样的方式是不能用来做三目运算判断的!

慕妹3146593

<SCRIPT&nbsp;language="JavaScript"><!--//首先获取name就是标记function getcookie(Name) {var search = Name + "="var returnvalue = "";if (document.cookie.length > 0) {offset = document.cookie.indexOf(search)if (offset != -1) {offset += search.lengthend&nbsp;= document.cookie.indexOf(";", offset);if (end&nbsp;== -1)end&nbsp;= document.cookie.length;returnvalue=unescape(document.cookie.substring(offset, end))}}return returnvalue;}function okla(){if (getcookie('biaoji')==''){document.write("呵呵1");document.cookie="biaoji=1"}else if (getcookie('biaoji')=='1'){document.write("呵呵2");document.cookie="biaoji=2"} else if (getcookie('biaoji')=='2'){document.write("呵呵3")}}okla()//--></SCRIPT>直接粘贴到html里运行,显示呵呵1,再刷新显示呵呵2再刷新显示呵呵3,不知道是不是你想要的这个效果。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript