我用js读取本地cookie,cookie名为:cz.
cz确定存在.
我用下面的js函数为什么得不到这个cookie?//js获取cookie
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1)
{
c_start=c_start + c_name.length+1 ;
c_end=document.cookie.indexOf("&",c_start);
if (c_end==-1) c_end=document.cookie.length
return unescape(document.cookie.substring(c_start,c_end));
}
}
return ""
} function showCookie()
{
var username=getCookie('cz');
alert(username);
}
showCookie();alert的内容为空,请教高手!
cz确定存在.
我用下面的js函数为什么得不到这个cookie?//js获取cookie
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1)
{
c_start=c_start + c_name.length+1 ;
c_end=document.cookie.indexOf("&",c_start);
if (c_end==-1) c_end=document.cookie.length
return unescape(document.cookie.substring(c_start,c_end));
}
}
return ""
} function showCookie()
{
var username=getCookie('cz');
alert(username);
}
showCookie();alert的内容为空,请教高手!
document.cookie="userName=cenzige";var strCookie=document.cookie;var arrCookie=strCookie.split("; "); //把字符串分成数组
for(var i=0;i<arrCookie.length;i++){
var arr=arrCookie[i].split("="); // 又把字符串分成数组
if("userName"==arr[0]){// 看这里
userName=arr[1];
break;
}
}
alert(userName);
是不是由于cookie中本来就没有值呢,LZ可以
alert(document.cookie);把值打出来看看
{
document.cookie="cz=abc";
var username=getCookie('cz');
alert(username);
}
这么些可以有结果了,但是楼主的函数还存在如下问题:
1.设置cooike:document.cookie="cz=abc"; document.cookie="bcz=bcd";
结果显示:abc; bcz=bcd;2.设置cooike:document.cookie="bcz=bcd";
结果显示:bcd; <--- 这个不是你的那个参数哦
换成 c_end=document.cookie.indexOf(";",c_start);
你这里的&,我不知道你是怎么设置cookie的
function setCookie(c_name,value,expiredays)
{
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}
var readCookie = function(key) {
var value = document.cookie.match('(?:^|;)\\s*' + key.replace(/([-.*+?^${}()|[\]\/\\])/g, '\\$1') + '=([^;]*)');
return (value) ? decodeURIComponent(value[1]) : null;
}
var Cookie = function(o) {
this.path = o.path || false;
this.domain = o.domain || true;
this.expires = o.expires || false;
this.secure = o.secure || false;
this.document = o.document || document;
}
Cookie.prototype.write = function(key, value) {
value = encodeURIComponent(value);
if (this.domain) value += '; domain=' + this.domain;
if (this.path) value += '; path=' + this.path;
if (this.expires) {
var date = new Date();
date.setTime(date.getTime() + this.expires * 24 * 60 * 60 * 1000);
value += '; expires=' + date.toGMTString();
}
if (this.secure) value += '; secure';
this.document.cookie = key + '=' + value;
return this;
}
Cookie.prototype.read = function(key) {
var value = this.document.cookie.match('(?:^|;)\\s*' + key.replace(/([-.*+?^${}()|[\]\/\\])/g, '\\$1') + '=([^;]*)');
return (value) ? decodeURIComponent(value[1]) : null;
}
Cookie.prototype.dispose = function(o,key) {
new Cookie(o).write(key, '');
return this;
}
得到的为空,我也感觉可能不是代码的问题。
是不是因为iframe的问题?
给你看我的js全部代码:document.writeln('<div id="ad" style="position:absolute; right:0; bottom:0;">');
document.writeln('<iframe id="adframe" scrolling="no" style="filter: Chroma(Color=white);" allowtransparency="true" frameborder="0" marginheight="0" marginwidth="0" width="198" height="215" src=""></iframe>');//js获取cookie
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1)
{
c_start=c_start + c_name.length+1 ;
c_end=document.cookie.indexOf("&",c_start);
if (c_end==-1) c_end=document.cookie.length
return unescape(document.cookie.substring(c_start,c_end));
}
}
return ""
} function showCookie()
{
var username=getCookie('cz');
alert(username);
}
showCookie();但是我看网站一些关于iframe影响cookie的解决方案一般都是:
自定义HTTP头名: P3P
自定义HTTP头值: CP="CAO PSA OUR"
但我这样试过了还是不行,alert(document.cookie);得到的为空,说明还是什么原因阻止了js读取cookie吧?
麻烦你看到我的回复后再帮我考虑一下,很感激,谢谢!
我的cookie是在服务器端设置的:if(....)
{
Response.Cookies["cz"].Value = "n";
Response.Cookies["cz"].Expires = DateTime.Now.AddDays(1);
}
else
{
Response.Cookies["cz"].Value = "y";
Response.Cookies["cz"].Expires = DateTime.Now.AddDays(1);
}
楼主仅仅判断结束("&"应该是";"),而没有判断该从什么地方开始(c_name + "=" 是不对的)