37

I use the jQuery cookie plugin for storing cookies, with the following code I can save a Cookie for 7 days, but it only saves it for the page its created on. I want the cookie to be available for the whole website.

$.cookie('basket',basket,{ expires: 7 });

I tried to set a path, but that didn't seem to work

$.cookie('basket',basket,{ expires: 7, path:'/' });

full code: this works fine, but it only saves the cookie for the current page

function add_to_basket(id,title){
if($.cookie('basket')){
    basket=$.cookie('basket');

    var basket_array = basket.split(',');

    var index = jQuery.inArray(id,basket_array);
    if(index > -1){
        return false;
    }else{
        basket+=','+id;
        $.cookie('basket',basket,{ expires: 7 });
    }
}else{

    basket=id;
    console.log(basket);
    $.cookie('basket',basket,{ expires: 7 });

}
4
  • What is basket? If it's an object you need to run it through JSON.stringify() first so you can store a string. Commented Feb 17, 2012 at 10:34
  • @waterschaats can you put more script Commented Feb 17, 2012 at 10:37
  • 2
    Your cookie setting code seems fine. If you check the cookie in Firefox it will tell you information such as the domain, path and expiry date. This may give you a clue what the problem is. Other browsers should also give you the data too. Commented Feb 17, 2012 at 10:38
  • reading the cookie seems to be the problem? Commented Feb 17, 2012 at 10:50

5 Answers 5

60

I just had the same problem. I fixed it by always specifying the path when writing the cookie.

$.cookie('basket', value, { path: '/' })

This is an issue with the jquery cookie plugin. It will default to the path of the current page.

Sign up to request clarification or add additional context in comments.

4 Comments

When I tried this method it actually set the cookie (in your case 'basket') to an object with a path property instead of retrieving the cookie.
That is for SETTING a cookie to the given path, not retrieval.
This is WRONG. It creates a cookie with the value of the second parameter.
I don't think this is true, I always write cookie with the path "/" property, I just noticed suddenly I have 2 cookies with same name - different values - it was created in "/" as I expected and miraculously also in path "/about"... Not always, but sometimes this happens.
16

In the plugin file change:

config.defaults = {};

to

config.defaults = {path:'/'};

from https://github.com/carhartl/jquery-cookie/issues/2#issuecomment-790288

Comments

1

I had the same problem but I found that this occurs only when I minify the jquery.cookie.js and when I put in

config.defaults = {expires: 180, path:'/', domain: '.domain.com' };

it sets the cookie path to '/', whatever internal page is loaded, e.g., yourdomain.com/en/page1/page - cookie path = '/'

Comments

1

I don't think that patching plugin's body is a nice idea. Sadly plugin is not configurable.. I use wrapper function:

$.cookie2 = function(key, value, options)
{
    if (typeof value!='undefined')
    { // setting cookie
        var defaults = {expires: 180, path:'/'};
        $.extend(defaults, options || {});
        return $.cookie(key, value, defaults);
    }
    // getting cookie
    return $.cookie(key, value, options);
}

Usage:

// set with defaults defined in wrapper
$.cookie2('name', 'value');

// rewrite defaults or add something
$.cookie2('name', 'value', {expires: 1, something: 'else'}); 

Comments

0

use this

$.cookie('basket', value, { path: '/' });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.