javascript - Cannot read property 'undefined' of undefined - Stack Overflow

Having a bit more trouble with Cookies, I did get my site to save cookies (to-do list items) and reload

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, and i is not yet defined, so myCookies[i] is asking for undefined on undefined. – 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
 |  Show 1 more ment

1 Answer 1

Reset to default 1

i 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信