Results 1 to 3 of 3

Thread: jQuery Cookie paths (to filter backwards and cover whole domain)

  1. #1
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default jQuery Cookie paths (to filter backwards and cover whole domain)

    I'm using a jQuery cookie to switch some special effects on and off - here's the code;
    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?
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    You are absolutely right about the residual cookies - I just booted up my laptop, re-added the path: '/' option and everything is fine
    Last edited by Beverleyh; 08-25-2010 at 05:14 PM. Reason: spelling
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •