Why are all my jQuery cookies turning up undefined?
Yeah, it sounds like it is because it's hosted locally.
See: Why does Chrome ignore local jQuery cookies?
Related videos on Youtube
Ian Zane
Updated on June 04, 2022Comments
-
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 andconsole.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 about 11 yearsHave 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 about 11 yearsJust a typo. Not in my actual code. Thanks for pointing that out, fixed.
-
Shankar Narayana Damodaran almost 9 yearspossible duplicate of Why does Chrome ignore local jQuery cookies?
-
-
Ian Zane about 11 yearsThat turned out to be it! Thank you for the help!
-
ArleyM almost 8 yearsOn a Vagrant / Symfony app this turned out to be true of :8080/app_dev.php as well! Thank you so much.