Why are all my jQuery cookies turning up undefined?

16,634

Yeah, it sounds like it is because it's hosted locally.

See: Why does Chrome ignore local jQuery cookies?

Share:
16,634

Related videos on Youtube

Ian Zane
Author by

Ian Zane

Updated on June 04, 2022

Comments

  • Ian Zane
    Ian Zane almost 2 years

    I've sourced jQuery, the jQuery Cookie Plugin, and my own Javascript (in that order) at the end of the body of my page. What I'm unable to grasp is why my cookie won't read when I console.log the cookie, or when I assign it to a variable and console.log the variable. Also, when I open the page in Chrome and look through the Resources>Cookies section, it shows that my page has created no cookies.

    Also, the problem may have to do with my operating on the file locally. It isn't hosted yet. Everything is just sitting on my drive, not in the internet yet.

    Here's my code:

    $.cookie('testcookie',1,{expires: 5, path:'/'});
    console.log($.cookie('testcookie'));
    var variable=$.cookie('testcookie');
    console.log(variable);
    

    That function should log '1' in the console twice, but just gives me two instances of undefined.

    I've checked my HTML four times now, and I'm 100% sure the sources for my scripts are pointed to the correct place. I've also made sure the function is being called correctly, etc. I've checked all the probable problems, but I just can't find anything wrong. Am I missing something?

    • Thomas C. G. de Vilhena
      Thomas C. G. de Vilhena about 11 years
      Have you noticed that you're missing an end quote at the second line of your sample code? Is that just a typo or a copy of your app code?
    • Ian Zane
      Ian Zane about 11 years
      Just a typo. Not in my actual code. Thanks for pointing that out, fixed.
    • Shankar Narayana Damodaran
      Shankar Narayana Damodaran almost 9 years
  • Ian Zane
    Ian Zane about 11 years
    That turned out to be it! Thank you for the help!
  • ArleyM
    ArleyM almost 8 years
    On a Vagrant / Symfony app this turned out to be true of :8080/app_dev.php as well! Thank you so much.