希望有人可以阐明这一点。我从逗号分隔的cookie(例如1,2,3,4,5)中获取值,并将其设置为一个变量。一切都很好,直到我尝试访问从数组中的变量设置为空,直到我刷新浏览器。有任何想法吗?JavaScript变量从数组null值直到浏览器刷新
//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
说明如何和你在哪里设置cookie – 2012-07-06 19:31:22
是什么'FirstSource'? – jbabey 2012-07-06 19:39:55
对不起 - 我更新了这个。这是遗留我把它改为'ExampleVar' – user1505467 2012-07-06 20:07:00