以下是本人写的代码,可是cookie保存不下来。请问该如何修改?<script>
//窗体初始
function window_init()
{
var treeHide = GetCookie("treeHide");
if(treeHide == "Y")
{
......
SetCookie("treeHide","N");
}
else
{
......
SetCookie("treeHide","Y");
}
} //取得cookie
function GetCookie(sName)
{
var aCookie = document.cookie.split(";");
for (var i=0; i<aCookie.length; i++)
{
var aCrumb = aCookie[i].split("=");
if (sName == aCrumb[0])
{
return aCrumb[1];
}
SetCookie(sName,"N");
} //创建或更新cookie
function SetCookie(sName,sValue)
{
var date = new Date();
//以下一行有问题,没有设置cookie的spires属性,可是用方法二也不行
document.cookie = sName + "=" + sValue ;
//方法二
// document.cookie = sName + "=" + sValue + ";spires = " + date.toGMTString();
} //取得cookie["treeHide"],同时更新cookie["treeHide"]的值
function navclck()
{
var treeHide = GetCookie("treeHide");
if(treeHide == "Y")
{
...
SetCookie(sName,"N");
}
else
{
...
SetCookie(sName,"Y");
}
}
</script>上面的SetCookie() GetCookie()方法有错误吗?还有可以通过SetCookie()方法进行cookie的更新吗?
//窗体初始
function window_init()
{
var treeHide = GetCookie("treeHide");
if(treeHide == "Y")
{
......
SetCookie("treeHide","N");
}
else
{
......
SetCookie("treeHide","Y");
}
} //取得cookie
function GetCookie(sName)
{
var aCookie = document.cookie.split(";");
for (var i=0; i<aCookie.length; i++)
{
var aCrumb = aCookie[i].split("=");
if (sName == aCrumb[0])
{
return aCrumb[1];
}
SetCookie(sName,"N");
} //创建或更新cookie
function SetCookie(sName,sValue)
{
var date = new Date();
//以下一行有问题,没有设置cookie的spires属性,可是用方法二也不行
document.cookie = sName + "=" + sValue ;
//方法二
// document.cookie = sName + "=" + sValue + ";spires = " + date.toGMTString();
} //取得cookie["treeHide"],同时更新cookie["treeHide"]的值
function navclck()
{
var treeHide = GetCookie("treeHide");
if(treeHide == "Y")
{
...
SetCookie(sName,"N");
}
else
{
...
SetCookie(sName,"Y");
}
}
</script>上面的SetCookie() GetCookie()方法有错误吗?还有可以通过SetCookie()方法进行cookie的更新吗?
spires<SCRIPT>
// Create a cookie with the specified name and value.
// The cookie expires at the end of the 20th century.
function SetCookie(sName, sValue)
{
date = new Date();
document.cookie = sName + "=" + escape(sValue) + "; expires=" + date.toGMTString();
}
</SCRIPT>
This example retrieves the value of the portion of the cookie specified by the sCookie parameter.Hide Example<SCRIPT>
// Retrieve the value of the cookie with the specified name.
function GetCookie(sName)
{
// cookies are separated by semicolons
var aCookie = document.cookie.split("; ");
for (var i=0; i < aCookie.length; i++)
{
// a name/value pair (a crumb) is separated by an equal sign
var aCrumb = aCookie[i].split("=");
if (sName == aCrumb[0])
return unescape(aCrumb[1]);
} // a cookie with the requested name does not exist
return null;
}
</SCRIPT>
This example deletes a cookie by setting its expires attribute to a past date. A cookie deleted in this manner might not be removed immediately by the browser.<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>
先谢谢楼上兄弟写的。
上述代码是visual studio 帮助中自带的,我也试过,不过cookie创建不了,也保存不了。
<script runat="server">
void Page_Load(object sender, System.EventArgs e)
{
if(Request.Cookies["aa"]!=null)
Response.Write(Request.Cookies["aa"].Value);
}
</script>
<SCRIPT>
SetCookie("aa","aaaaaa")
alert(GetCookie("aa"))
function SetCookie(sName, sValue)
{
date = new Date("2008/12/1");
document.cookie = sName + "=" + escape(sValue) + "; expires=" + date.toGMTString();
}function GetCookie(sName)
{
// cookies are separated by semicolons
var aCookie = document.cookie.split("; ");
for (var i=0; i < aCookie.length; i++)
{
// a name/value pair (a crumb) is separated by an equal sign
var aCrumb = aCookie[i].split("=");
if (sName == aCrumb[0])
return unescape(aCrumb[1]);
} // a cookie with the requested name does not exist
return null;
}function DelCookie(sName)
{
document.cookie = sName + "=" + escape(sValue) + "; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}
</SCRIPT>
比如,原先 cookie["aCookie"] = "yes",现在我想通过JS, 把 cookie["aCookie"] = "no",
可以实现吗?可以的话,如何实现 ?