PDA

View Full Version : jQuery Cookie paths (to filter backwards and cover whole domain)



Beverleyh
08-25-2010, 03:22 PM
I'm using a jQuery cookie to switch some special effects on and off - here's the code;

// Cookie - turn special effects off/on switch
jQuery('#fxoff').click(
function () {
jQuery('#fxoff').css({'display':'none'});
jQuery('#fxon').css({'display':'block'});
// do stuff - hide all effects
jQuery.cookie('fx', 'off', { expires: 7 });
}
);

jQuery('#fxon').click(
function () {
jQuery('#fxoff').css({'display':'block'});
jQuery('#fxon').css({'display':'none'});
// do stuff - show all effects
jQuery.cookie('fx', 'off', { expires: -1 });
}
);

var fx = jQuery.cookie('fx');
if (fx == 'off') {
jQuery('#fxoff').css({'display':'none'});
jQuery('#fxon').css({'display':'block'});
// do stuff - hide all effects
};

The #fxoff and #fxon switches appear on all pages of my site including the ones in sub-folders.

If the switch is activated on the home page, the cookie is recognised across the whole site and shows on or off accordingly on every page. Perfect!

BUT If the switch is pressed on a page in a subfolder, the cookie is only recognised on pages in that subfolder and doesnt filter backwards back to the root level.

I tried setting path:'/' but it doesnt seem to have any impact.

I'd like the cookie to be set, recognised and deleted from any page, no matter what level it's at (site-wide recognition).

Can anybody please offer assistance? What am I missing with the path?

jscheuer1
08-25-2010, 03:55 PM
The first thing to make sure of is that you delete all cookies, and that your calls to jQuery.cookie all now include the path: '/' option. If the folder specific cookies are still hanging around, they will have precedence in their folders over the domain cookie.

In most browsers this can be done by opening only one instance of the browser, navigating to a blank page and using the options > security menu or something similar, deleting all cookies. Some browser add-ons make this process simpler.

Make sure you are using the path: '/' option correctly as described for your version of jQuery.cookie, and that it has such an option.

If you are inclined to diagnose this for yourself, IE 8 developer toolbar (comes with most IE 8 installations, hit F12 while viewing the page, choose Cache > View Cookie Information) has an option to display cookies, which works quite well. The equivalent in Firefox developer tools add-on only works for live pages. I don't use Firebug, but it may have a similar tool that may or may not work locally.

There may be other possibilities, but most likely you either have old folder specific cookies hanging around, and/or some of your code is still making new ones, and/or the jQuery cookie code you are using is flawed.

If none of that that gets things working for you, give us a link to the page.

Beverleyh
08-25-2010, 05:12 PM
You are absolutely right about the residual cookies - I just booted up my laptop, re-added the path: '/' option and everything is fine :)