Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are all my jQuery cookies turning up undefined?

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?

like image 215
Ian Zane Avatar asked Jan 15 '23 04:01

Ian Zane


1 Answers

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

See: Why does Chrome ignore local jQuery cookies?

like image 100
Julie Avatar answered Jan 16 '23 21:01

Julie