Search code examples
jquerycookiesjquery-cookie

$.cookie Error writing value


I recent started to use jquery cookie, downloaded from https://github.com/carhartl/jquery-cookie

But lately, i have failed to write or read the cookie. Here is a piece of my code:

$.cookie.raw = true;
$.cookie.json = true;
$('select').select2().on("select2-selecting", function(e) {
    var selval=e.val;
    alert(selval); # alerts perfect value
    if(typeof $.cookie("del") === "undefined")
    {
        alert("blank"); # alerts blank
        $.cookie('del', selval);
        alert($.cookie("del")); # alerts undefined
    }
    else
    {
        alert("not blank");
        var del=$.cookie("del");
        del=del+"+"+selval;
        $.cookie("del",del);
    }
    alert($.cookie("del")); # alerts undefined
});

Solution

  • Oh man, I lost 2 hours on this one about a week ago. Giving you my timeline is easiest - one of these should help you :P

    1. Start out using https://github.com/carhartl/jquery-cookie
    2. Run into troubles - cookie issues, IE crashes null values, etc.
    3. Somehow figure out a massive conflict with TableSorter (if you use this, just download same version fresh)
    4. Run into more problems - writing dates, dateToUTC() errors & some other oddball stuff.
    5. Use https://gist.github.com/mathiasbynens/399854 instead

    Then lastly - I found (nub discovery I'm sure), that writing any boolean value is not a winner. going back to 'true' and 'false' feels weird, but a false value null'd the cookie value and true kinda emptied it.

    Obviously, I'm not really good with this stuff, but the cookie object was taking on the bool, not the value thereof.