tags:

views:

41

answers:

2

I have a cookie that is on every page of my site. It works fine when the address is like this. http://mydomain.com/index.html or whatever.

but when I have a page that is in a folder like this http://mydomain.com/folder/page.html

instead of using the cookie that I have set for all the other pages it creates a new cookie just for that folder. Is there any way to keep the same cookie for all folders? Or I'm I just doing something terrible wrong?

Thanks

my code -- I have this in a external js. file

$(document).ready(function(){
    var cookie = $.cookie('cookiename');
    if (cookie) {
    }
    else {
     $.cookie('cookiename', 'cookievalue');
    }
});

$(document).ready(function() {
    $('.watevevever').click(function() {
    var va = $('#watev').css('display');
     if (va == 'none'){
      $('#watev').fadeIn("slow");
      $.cookie('cookiename', 'cookievalue');
     }
     else {
      $('#watev').fadeOut("slow");
      $.cookie('cookiename', 'cookievalue');
     }
    });
    var va = $.cookie('cookiename');
    if (va == 'cookievalue') {
     $('#watev').css("display","none");
    };
});
+1  A: 
$.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });

the optional parameters includes 'path' .. which should be '/'

Sabeen Malik
+2  A: 

If you are using this plugin for jQuery (and its source is here), it seems, by looking at the source, that you can pass some additional parameters as an object, as a third parameter to the $.cookie method.


For instance, from this tutorial, you can add an expiration date :

$.cookie('the_cookie', 'the_value', { expires: 7 }); // set cookie with an expiration date seven days in the future

Looking at the source, you have this portion of code :

var path = options.path ? '; path=' + (options.path) : '';
var domain = options.domain ? '; domain=' + (options.domain) : '';
var secure = options.secure ? '; secure' : '';


So, I suppose you can use a "path" attribute in the object given as third parameter, like, for instance :

$.cookie('the_cookie', 'the_value', {path: '/'});

Of course, this '/' is if you want to set to cookie for every paths on your domain -- which seems to be the case.


You can probably also set some other options, like 'domain', if you want to use subdomains, btw...

Pascal MARTIN
This worked great! Thanks +1 :)
PHPNooblet