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 comment 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’.
ishould be global, I have made ittotalCount.The first section should be:
Second section should be: