cookie 读写..

C_sdnElf 2008-12-20 10:19:38
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<script type = "text/javascript">
function setCookie(sName,sValue,oExpires,sPath,sDomain,bSecure)
{
var sCookie = sName + "=" + encodeURIComponent(sValue);
if(oExpires)
{
alert(oExpires);
sCookie += "; expires=" + oExpires.toGMTString();
}
if(sPath)
{
sCookie += "; path=" + sPath;
}
if(sDomain)
{
sCookie += "; domain=" + sDomain;
}
if(bSecure)
{
sCookie += "; secure";
}
document.cookie = sCookie;
}

function getCookie(sName)
{
var sRE = "(?:; )?" + sName + "=([^;]*);?";
var oRE = new RegExp(sRE);

if(oRE.test(document.cookie))
{
return decodeURIComponent(RegExp["$1"]);
}
else
{
return null;
}
}

function deleteCookie(sName,sPath,sDomain)
{
setCookie(sName,"",new Date(0),sPath,sDomain);
}



setCookie("name","Nicholas");
setCookie("mm","Professional JavaScript",new Date(Date.parse("Jan 1, 2008")));
setCookie("message","Hello World!","Jan 1, 2008","/books","http://www.wrox.com",true);

var sName = getCookie("name");
var sBook = getCookie("mm");
var sMessage = getCookie("message");

alert(sName);
alert(sBook);/*null读不出值*/
alert(sMessage);/*null读不出值*/
</script>
</HEAD>

<BODY>
</BODY>
</HTML>

在cookie里设置信息,再读出来,可是,我只是设置名字和值能正确读出来,可是其他的就读不出来。

不知道哪里出了问题
...全文
64 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
tommir3 2008-12-20
  • 打赏
  • 举报
回复
太帅了
mengxj85 2008-12-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 asdfgh_5982 的回复:]
测试了一下,传入的时间有问题。换一个Cookie的操作方式吧。你写的不怎么样

JScript code
//function to decode Cookie then get Cookie value
function GetCookieVal(offset)
{
var endstr = document.cookie.indexOf (":", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
//set Cookie value
function SetCookie…
[/Quote]
楼上好强,顶
asdfgh_5982 2008-12-20
  • 打赏
  • 举报
回复
测试了一下,传入的时间有问题。换一个Cookie的操作方式吧。你写的不怎么样

//function to decode Cookie then get Cookie value
function GetCookieVal(offset)
{
var endstr = document.cookie.indexOf (":", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
//set Cookie value
function SetCookie(name, value)
{
var expdate = new Date();
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
if(expires!=null) expdate.setTime(expdate.getTime() + ( expires * 1000 ));
document.cookie = name + "=" + escape (value) +((expires == null) ? "" : ("; expires="+ expdate.toGMTString()))
+((path == null) ? "" : ("; path=" + path)) +((domain == null) ? "" : ("; domain=" + domain))
+((secure == true) ? "; secure" : "");
}
//function:delete Cookie
function DelCookie(name)
{
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetMyCookie (name);
document.cookie = name + "=" + cval + "; expires="+ exp.toGMTString();
}
// Function to return the value of the cookie specified by "name".
// name - String object containing the cookie name.
// returns - String object containing the cookie value, or null if
// the cookie does not exist.
function GetNoEscapeCookie(name) {
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1)
{
begin = dc.indexOf(prefix);
if (begin != 0) return null;
}
else
begin += 2;

var end = document.cookie.indexOf(";", begin);
if (end == -1) end = dc.length;
return dc.substring(begin + prefix.length, end);
}
asdfgh_5982 2008-12-20
  • 打赏
  • 举报
回复
写入Cookie的特殊字符请先编码特殊字符入‘/’ 可以试着用escape或者其他的

87,915

社区成员

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

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