function changesex(){
var cookie=document.cookie;
var sex;
//alert(cookie);
cooarr=cookie.split(";");
for (var i=0;i<cooarr.length;i++){
coo=cooarr[i].split("=");
if (coo[0]=="sex"){
sex=coo[1];
break;
}
}
if (sex=="male") sex="famale";
else sex="male";
var date=new Date();
date.setTime(date.getTime()+3600*24*30);
document.cookie="sex="+sex+";expires="+date.toGMTString();
return sex;
}
this javascript function is used to change a cookie content,but I find a problem that this expression
if (coo[0]==”sex”){
can be true only if i==0 and coo[0] equals the following string.
so the problem is the if expression can’t be true even if coo[0] equals “xxx” string.
I spent a couple of hours on this and I was confused.
I got the answer when we got document.cookie and split by “;”,their happen to be a space in front of the cookie name string,for example “sex” will become ” sex”,I don’t know the exactly reason but I can’t handle cookie now.