Why does jQuery cookie not actually set a cookie? Why does jQuery cookie not actually set a cookie? json json

Why does jQuery cookie not actually set a cookie?


It's probably the fact the file is on your desktop that's causing the problem. Browsers normally behave by serving up cookies based on the domain they were received from and their path.

You may not be able to read the cookie immediately after setting it: Writing a cookie involves setting headers in a HTTP request and, likewise, reading them involves reading headers in a HTTP response.

Try hosting your page on a web-server and see if that works for you.


If you are having troubles with the cookies plugin why not just make up your own cookie functions? Read, Write and (optional) delete.

var createCookie = function(name, value, days) {    if (days) {        var date = new Date();        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));        var expires = '; expires=' + date.toGMTString();    }    else var expires = '';        document.cookie = name + '=' + value + expires + '; path=/';};var readCookie = function(name) {    var nameEQ = name + '=';    var ca = document.cookie.split(';');    for (var i = 0; i < ca.length; i++) {        var c = ca[i];        while (c.charAt(0) == ' ') c = c.substring(1, c.length);        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);    }    return null;};var eraseCookie = function(name) {    createCookie(name, '', -1);};

I cannot comment on the specific plugin as I have never used it.. however these functions all work and have been tested.

So for your example:

createCookie("people", JSON.stringify(people_obj_array), 7); // storealert(readCookie("people")); // retrieveeraseCookie("people"); // removealert(readCookie("people")); // oo look i'm no longer here.


From my research jquery.cookie.js is fairly old, and doesn't seem to be maintained any longer. You might have better luck using this library instead. Its description on Google Code is "Javascript Cookie Library with jQuery bindings and JSON support", and includes methods for everything you're trying to do!