SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jQuery Cookie Not Setting

    Hi all,

    I'm using the jQuery Cookie Plugin for a theme switcher, http://stilbuero.de/jquery/cookie/, and for some reason i can't get it to set.

    Here's what i got.

    Code:
    $("a.new-theme").click(function() {
    		var newtheme = $(this).attr("rel");
    		$("#new-theme").attr({ href: "http://127.0.0.1/blog/blog_html/w01/" + newtheme + "/css/style.css"});
    		$.cookie('selected_theme', newtheme, { path: '/', expires: 7 });
    		return false;
    	});
    Is it possible the path is incorrect? I've tried setting an absolute path to the site folder with no luck.
    I'm also calling the plugin before my function, so that's not a problem.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    You are testing this locally.

    If you're using Google Chrome for your web browser, the browser doesn't support local cookies.
    You can though use a command-line option to enable them

    --enable-file-cookies


    More command line options for google chrome are at: http://www.ericdlarson.com/misc/chro...ine_flags.html

    And a possible rationale for not having local cookies: http://www.google.com/support/forum/...855c0f17&hl=en
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ahhh that makes perfect sense.
    Thanks so much!

  4. #4
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So I've uploaded to my external server, which also runs apache, with no luck.
    The cookie is not setting. So it must be something else, unfortunately.

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    We do now know what it is not, which makes further diagnosing easier to perform
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Exactly! And thanks again for all of your help.

    So here's what i've got. I've tried multiple different variations of setting the cookie but still with no luck.

    I've set an alert to see if the cookie is setting , which it is! But each time I reload the page, it goes back to the default style, even with the cookie in place.

    So for a second I thought that my stylesheet href was incorrect. I figured I should have it populated. But when i removed the default href, it would set to the selected style, then remove itself on reload.

    For the theme switcher, I'm targeting the href of my style reference. Here's my set default.
    Code:
    <link rel="stylesheet" id="newhref" type="text/css" href="/wp-content/themes/test_a/css/style.css" />
    with:

    Code:
    $("a.selecttheme").click(function() {
    		var newtheme = $(this).attr("rel");
    		$("#newhref").attr({ href: "/wp-content/themes/" + newtheme + "/css/style.css"});
    		$.cookie('new_theme', newtheme,  { path: '/', expires: 10 });
    		return false;
    	});
    With the cookie setting correctly, I believe there's something wrong with my default style reference - but nothing I've tried is working out.

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    How are you checking that the cookie is bring correctly read when the page loads. perhaps we can narrow down the issue to either one about the cookie, or one about switching the style.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm pulling it up via an alert on load. It's been returning the name of the last selected theme each time, but the incorrect theme shows

    Code:
    alert( $.cookie("new_theme") );

  9. #9
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Then it seems that the cookie is working well, and it's an issue with switching styles that is at hand.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  10. #10
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I change the link's href, then set the cookie, should that not keep the link as is? Is it a mistake to have an href initially set?

  11. #11
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh wow... i'm silly as hell.
    I wasn't pulling the cookie value into the stylesheet href. No wonder it was resetting on refresh.

    Code:
    <link rel="stylesheet" id="new-theme" type="text/css" href="/wp-content/themes/<? echo $_COOKIE["new_theme"]; ?>/css/style.css" />
    Thanks for your help

  12. #12
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now my issue is setting a default stylesheet if no cookie has been set, and if a cookie is set, that value is removed so it's doesn't rewrite the cookie on reload.


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
  •