希望有人可以阐明这一点。我正在从逗号分隔的 cookie 中获取值(例如 1、2、3、4、5),并将其设置为变量。一切都很好,直到我尝试访问从数组中设置的变量,这些变量在我刷新浏览器之前为空。有任何想法吗?
//Setting ExampleVar Cookie
var ExampleVar = null;
var ExampleVar = readCookie('SomeCookie');
if (ExampleVar == null) {
createCookie('SomeCookie',ValuesVariable,365)//create cookie if null
}
var ExampleVar = null;
var ExampleVar = readCookie('CookieGoesHere');//e.g. 1,2,3,4,5
document.write (ExampleVar);//Prints out 1,2,3,4,5 as it should
var myArr = new Array();
var myArr = ExampleVar.split(",");
for(var i=0;i<myArr.length;i++){
document.write("Array Index " + i + " = " + myArr[i] + "<br />");//prints null until browser is refreshed
}
var Foo = myArr[0];//set to Null until browser is refreshed
var Bar = myArr[1];//set to Null until browser is refreshed