Having a bit more trouble with Cookies, I did get my site to save cookies (to-do list items) and reload them after the page had been loaded and place them back into the table on screen.
But, when I deleted all the cookies and tried to start fresh I keep getting a console error saying Cannot read property 'undefined' of undefined
Now, if I ment out the below two sections of code, this problem goes away, but the cookie system will fail of course.
This section checks for the number of items that were in the to-do-list last time someone visited, and sets i
as that number, so nothing get's deleted.
var listCookies = document.cookie.split(";");
for(var a = 0; a < listCookies.length; a++) {
var listCount = myCookies[i].trim().split("=");
if(listCount[0].indexOf("listCount") === 0) {
var i = listCount;
} else {
var i = 0;
}
}
This section finds the actual to-do's themselves and places them in the table on screen.
var myCookies = document.cookie.split(";");
for(var b = 0; b < myCookies.length; b++) {
var cookie = myCookies[i].trim().split("=");
if(cookie[0].indexOf("todo-") === 0) {
window.todoTable.insertAdjacentHTML('beforeend', decodeURIComponent(cookie[1]));
}
}
I understand the issue is that there aren't any cookies saved on someones first visit, but when I tried to add an if (typeof document.cookie === "undefined")
statement that didn't seem to do anything.
I'm assuming I need an if statement wrapped around these pieces of code right? To say, if there isn't a cookie, 'skip this bit'. If there is, 'do stuff'.
Having a bit more trouble with Cookies, I did get my site to save cookies (to-do list items) and reload them after the page had been loaded and place them back into the table on screen.
But, when I deleted all the cookies and tried to start fresh I keep getting a console error saying Cannot read property 'undefined' of undefined
Now, if I ment out the below two sections of code, this problem goes away, but the cookie system will fail of course.
This section checks for the number of items that were in the to-do-list last time someone visited, and sets i
as that number, so nothing get's deleted.
var listCookies = document.cookie.split(";");
for(var a = 0; a < listCookies.length; a++) {
var listCount = myCookies[i].trim().split("=");
if(listCount[0].indexOf("listCount") === 0) {
var i = listCount;
} else {
var i = 0;
}
}
This section finds the actual to-do's themselves and places them in the table on screen.
var myCookies = document.cookie.split(";");
for(var b = 0; b < myCookies.length; b++) {
var cookie = myCookies[i].trim().split("=");
if(cookie[0].indexOf("todo-") === 0) {
window.todoTable.insertAdjacentHTML('beforeend', decodeURIComponent(cookie[1]));
}
}
I understand the issue is that there aren't any cookies saved on someones first visit, but when I tried to add an if (typeof document.cookie === "undefined")
statement that didn't seem to do anything.
I'm assuming I need an if statement wrapped around these pieces of code right? To say, if there isn't a cookie, 'skip this bit'. If there is, 'do stuff'.
Share Improve this question asked Jan 9, 2013 at 15:47 lukeseagerlukeseager 2,62511 gold badges40 silver badges59 bronze badges 6- 1 In the first section you seem to be referencing i and myCookies variables before they were even set. Is that right? var listCount = myCookies[i].trim().split("="); – Claudio Commented Jan 9, 2013 at 15:52
- Can you check which line caused the error? – ATOzTOA Commented Jan 9, 2013 at 15:53
-
It should be
if (typeof( document.cookie) === "undefined")
. – ATOzTOA Commented Jan 9, 2013 at 15:55 -
myCookies
is not defined, andi
is not yet defined, somyCookies[i]
is asking forundefined
onundefined
. – apsillers Commented Jan 9, 2013 at 16:05 - Why do you need the first section? What do you mean by "nothing gets deleted" ? – ATOzTOA Commented Jan 9, 2013 at 16:10
1 Answer
Reset to default 1i
should be global, I have made it totalCount
.
The first section should be:
var totalCount = 0;
var listCookies = document.cookie.split(";");
for(var a = 0; a < listCookies.length; a++) {
var listCount = listCookies[a].trim().split("=");
if(listCount[0].indexOf("listCount") === 0) {
totalCount = listCount[1];
break;
}
}
Second section should be:
var myCookies = document.cookie.split(";");
for(var b = 0; b < myCookies.length; b++) {
var cookie = myCookies[b].trim().split("=");
if(cookie[0].indexOf("todo-") === 0) {
window.todoTable.insertAdjacentHTML('beforeend', decodeURIComponent(cookie[1]));
}
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745365201a4624544.html
评论列表(0条)