我的代码中有一个setCookie和getCookie函数。它们在IE和FireFox中运行得非常好,但在Chrome中则不然。基本上我的脚本检查是否检查了某些复选框,然后根据组合,在cookie中存储了一个名为" layer的特定值。"我收到了存储在"层中的值的警报,"但它总是出现在" undefined"在Chrome中。我已经检查了我的设置,并且我已经允许Cookie并尝试清除它们并再次访问它们。但它没有工作!功能本身绝对没有错,因为它们在其他浏览器中运行良好......它只是Chrome。
function setCookie(cname,value,exdays)
{
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toGMTString();
document.cookie = cname + "=" + value + "; " + exdays + "; ";
}
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name)
{
return unescape(y);
}
}
}
var ch1 = document.getElementById("checkbox-h-2a").checked;
var ch2 = document.getElementById("checkbox-h-2b").checked;
var ch3 = document.getElementById("checkbox-h-2c").checked;
if (ch1==true && ch2==false && ch3==false) {
setCookie("layer","hosp",15);
}
else if (ch1==true && ch2==true && ch3==false) {
setCookie("layer","hospFire",15);
}
else if (ch1==true && ch2==false && ch3==true) {
setCookie("layer","hospPolice",15);
}
else if (ch1==false && ch2==true && ch3==true) {
setCookie("layer","firePolice",15);
}
else if (ch1==false && ch2==true && ch3==false) {
setCookie("layer","fire",15);
}
else if (ch1==false && ch2==false && ch3==true) {
setCookie("layer","police",15);
}
else if ((ch1==false && ch2==false && ch3==false) || (document.getElementById("checkbox-h-2c").checked==true && ch2==true && ch3==true)) {
setCookie("layer","hospFirePolice",15);
}
alert(getCookie("layer"));
window.location = FACILITY_MAP;