我在 JavaScript 中创建了一个 cookie。我可以看到并使用该文件中的 cookie。我无法在同一域的另一个页面中查看和使用相同的 cookie。可能是什么问题呢?
这是代码
// Code for set Cookie
// Code for set Cookie
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
//Function to call setcookie. In this function I cookie is adding successfully and I can alert the value of the cartcounter cookie. It will be increment one by one for each click
function makeSure(skey,name){
var cartcounter=getCookie("cartcounter");
cartcounter=parseInt(cartcounter);
chk=0;
for(var i=1;i<=cartcounter;i++){
var ckey = getCookie(i+"_skey");
if(ckey==skey){
chk++;
}
}
if(chk==0){
cartcounter=cartcounter+1;
setCookie("cartcounter",cartcounter,365);
setCookie(cartcounter+"_skey",skey,365);
setCookie(cartcounter+"_name",name,365);
setCookie(cartcounter+"_val",$("#cnt_"+skey).val(),365);
alert(name+" added to your cart successfully.. ");
}
else
alert("You have already added "+name+" item to cart ");
}
// This is the code in another page to view the cookie. But it show only 0
var cartcounter=getCookie("cartcounter");
alert("Counter="+cartcounter);