当前位置: 代码迷 >> JavaScript >> 用js这样的cookies如何操作
  详细解决方案

用js这样的cookies如何操作

热度:196   发布时间:2012-02-20 21:18:24.0
用js这样的cookies怎么操作?
用js实现如vbs中   response.cookies( "user ")( "cityid ")=101000
和request.cookies( "user ")( "cityid ")

在页面顶部   我先判断是否有cookies存在,有则不变,没有则用vbs写入cookies,代码如下
cityid=request.cookies( "user ")( "cityid ")
if   cityid= " "   or   cityid   then
cityid=110000
cityname= "杭州 "
response.cookies( "user ")( "cityid ")=cityid
response.cookies( "user ")( "cityname ")=cityname
response.cookies( "user ").domain= ".user.com. "
Response.Cookies( "user ").Secure   =   False
end   if

在前台页面上放了个下拉框,切换下拉框时触发js写cookies值,实现功能如以上代码。
然后再用asp取出cityid

谢谢

------解决方案--------------------
document.cookie
------解决方案--------------------
function setCookie(name, value)//cookies设置
{

var argv = setCookie.arguments;//获取参数数组
var argc = setCookie.arguments.length;//
var expires = (argc > 2) ? argv[2] : null;//是否设置cookies日期
if(expires!=null)
{
var LargeExpDate = new Date ();
LargeExpDate.setTime(LargeExpDate.getTime() + (expires*1000*3600*24));
}

document.cookie = name + "= " + escape (value)+ "; "+((expires == null) ? " " : ( "; expires= " +LargeExpDate.toGMTString()))

}

function getCookie(Name)//cookies读取
{
var search = Name + "= "
if(document.cookie.length > 0)
{

offset = document.cookie.indexOf(search)
if(offset != -1)
{
offset += search.length
end = document.cookie.indexOf( "; ", offset)
if(end == -1) end = document.cookie.length
return unescape(document.cookie.substring(offset, end))
}
else return " "
}
return " "
}
  相关解决方案