对document.cookie的操作汇集;另外如何删除cookie?

qiushuiwuhen 2002-05-24 08:10:37
1.添加一个新的
document.cookie="新的name=value"

2.修改原有的
document.cookie="原有的name=value"

3.查询某个name的值
<script>
alert(getCookieByName("login"))
alert(getCookieByName("login2"))
function getCookieByName(name){
/********(qiushuiwuhen)***********/
var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
if(arr=document.cookie.match(reg))
return arr[2];
else
return null
}
</script>

4.对中文和;的处理
一般用escape,如
document.cookie="name="+escape(含中文的字符串)
但提交给php,asp等无法处理,所以建议使用urlencoding,如

<script language="vbscript">
function urlencoding(vstrin)
dim i,strreturn
strreturn = ""
for i = 1 to len(vstrin)
thischr = mid(vstrin,i,1)
if abs(asc(thischr)) < &hff then
strreturn = strreturn & thischr
else
innercode = asc(thischr)
if innercode < 0 then
innercode = innercode + &h10000
end if
hight8 = (innercode and &hff00)\ &hff
low8 = innercode and &hff
strreturn = strreturn & "%" & hex(hight8) & "%" & hex(low8)
end if
next
urlencoding = strreturn
end function
</script>
<script language=javascript>
document.cookie="name="+urlencode(含中文的字符串)
</script>
这样提交到php,asp等就可以用urlcode解析出来了了



现在最大问题是如何删除一个cookie?
...全文
1223 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
saucer 2002-05-25
  • 打赏
  • 举报
回复
from MSDN documentation:
http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/cookie.asp?frame=true

<SCRIPT>
// Delete the cookie with the specified name.
function DelCookie(sName)
{
document.cookie = sName + "=" + escape(sValue) + "; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}
</SCRIPT>
jsidiot 2002-05-25
  • 打赏
  • 举报
回复
申明一下。我是来蹭分的
我什么也不会
chinaillboy 2002-05-24
  • 打赏
  • 举报
回复
娶回?
Lostinet 2002-05-24
  • 打赏
  • 举报
回复
把cookie的get和set分开来看待就好了。
document.cookie="..."
相当于向cookie提交一个信息。具体的操作根据name,path,expires来操作。
xxx=document.cookie则娶回所有的这个信息组。
weidegong 2002-05-24
  • 打赏
  • 举报
回复
2.修改原有的
document.cookie="原有的name=" 这样算不算删除了?
qiushuiwuhen 2002-05-24
  • 打赏
  • 举报
回复
上面是删除所有,我想即时删除其中的某个cookie
weidegong 2002-05-24
  • 打赏
  • 举报
回复
function SetCookie (name, value) {
var expire_days = 30;//(保存的天数)
var expire_date = new Date(); //new Date("December 31, 3666");//长期有效的办法
var ms_from_now = expire_days*24*60*60*1000;
expire_date.setTime(expire_date.getTime() + ms_from_now);
var expire_string = expire_date.toGMTString();
document.cookie = name + "=" + escape (value)+ ";expires=" +expire_string;
}
qiushuiwuhen 2002-05-24
  • 打赏
  • 举报
回复
其实解不解决并不是关键,重在讨论和开心,来者有分,嘿嘿
thinkeasy 2002-05-24
  • 打赏
  • 举报
回复
不会了,蹭个分

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧