document.cookie = "name="+value
2、写个函数动态调用
//设置或添加cookie
function setCookie(name,value,time){
var str = name + "=" + escape(value)
if(time >0){
var date = new Date()
var ms = time*3600*1000
date.setTime(date.getTime() + ms)
str += "expires=" + date.toGMTString()
}
document.cookie = str
}
//获取cookie
function getCookie(name){
//cookie中的数据都是以分号加空格区分开
var arr = document.cookie.split("")
for(var i=0i<arr.lengthi++){
if(arr[i].split("=")[0] == name){
return arr[i].split("=")[1]
}
}
//未找到对应的cookie则返回空字符串
return ''
}
//删除cookie
function removeCookie(name){
document.cookie = name+"=expires="+(new Date(0)).toGMTString()
}
你可以手动设置,Document属性可以返回一个HtmlDocument的类,他有Cookie属性,不过他只是个字符串,建议你看一下cookie的格式然后把CookiesContainer的内容读出来,接到cookie后面再付给Cookie属性你可以搜搜javascript如何在页面上设置和修改cookie,照葫芦画瓢应该没问题
你设置的cookie默认就会存在本地当中。给你一段设置cookie字段值希望对你有帮助 HttpCookie myCookie = HttpContext.Current.Request.Cookies[name]if (myCookie == null)
myCookie = new HttpCookie(name)
else if (myCookie.Values.Count >9)
myCookie.Values.Remove(myCookie.Values.Keys[0])
int count = myCookie.Values.Count
for (int j = 0j <countj++)
{
if (HttpUtility.UrlDecode(myCookie.Values[j]) == value)
{
myCookie.Values.Remove(myCookie.Values.Keys[j])break
}
}myCookie.Values.Add(DateTime.Now.ToString("yyMMddHHmmss")+new Random().Next(100).ToString(), HttpUtility.UrlEncode(value))
myCookie.Domain = "naoqiu.com"
myCookie.Expires = DateTime.Now.AddDays(365)
HttpContext.Current.Response.AppendCookie(myCookie)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)