Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Cookie only serving pages on root directory?!

  1. #1
    Join Date
    Mar 2009
    Location
    Calgary, Alberta
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post Cookie only serving pages on root directory?!

    Hello there...I have this cookie for a multi-themed website. The cookie works great except it only works on pages that are on my root directory. Can someone please tell me how I can modify it to serve the purpose on all pages in my website?

    Code:
    // JavaScript Document
    function getCookie(c_name)
    {
    if (document.cookie.length>0)
      {
      c_start=document.cookie.indexOf(c_name + "=");
      if (c_start!=-1)
        { 
        c_start=c_start + c_name.length+1; 
        c_end=document.cookie.indexOf(";",c_start);
        if (c_end==-1) c_end=document.cookie.length;
        return unescape(document.cookie.substring(c_start,c_end));
        } 
      }
    return "";
    }
    
    function setCookie(c_name,value,expiredays)
    {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
    }

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Please post a link to the page on your site that contains the problematic script so we can check it out.

    See these threads for proper posting format when asking a question.
    http://www.dynamicdrive.com/forums/showthread.php?t=6
    http://www.dynamicdrive.com/forums/s...ad.php?t=19533
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Mar 2009
    Location
    Calgary, Alberta
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    The problem occurs on the following two pages:

    http://www.s239171386.onlinehome.us/contact/index.php
    http://www.s239171386.onlinehome.us/...lery/index.php

    When setting a theme color in one page the cookie does not bring up that preference on the other pages. I think that the cause of this is that these two pages are not on the root directory of the website.

  4. #4
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    In your setcookie function, add the highlighted below:

    Code:
    function setCookie(c_name,value,expiredays)
    {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+";path=/";
    }
    Not tested, but should work. Hope this helps.

    Edit: After looking at your page again, I notice that you do not specify the expiredays variable for the setcookie function. If you edit your function to the following, it should work this time.

    Code:
    function setCookie(c_name,value)
    {
    var exdate=new Date();
    exdate.setDate(exdate.getDate());
    document.cookie=c_name+ "=" +escape(value)+";expires="+exdate.toGMTString()+";path=/";
    }
    Hope this helps.
    Last edited by thetestingsite; 04-19-2009 at 02:13 AM.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  5. #5
    Join Date
    Mar 2009
    Location
    Calgary, Alberta
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Negative! I thought of this exact thing too, but for some strange reason it is not working.

    Any other ideas?


    PS: I really appreciate this

  6. #6
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    I've edited the post above, and this time it should work. Let me know if not though.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  7. #7
    Join Date
    Mar 2009
    Location
    Calgary, Alberta
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Apparently there's a syntax error on the line in red below I can't figure out what it is:

    Code:
    function setCookie(c_name,value)
    {
    var exdate=new Date();
    exdate.setDate(exdate.getDate());
    document.cookie=c_name+ "=" +escape(value)+";expires="+exdate.toGMTString())+";path=/";
    }

  8. #8
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Sorry, there was an extra ) in the code after I edited it. Below is correct for that line:

    Code:
    document.cookie=c_name+ "=" +escape(value)+";expires="+exdate.toGMTString()+";path=/";
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  9. #9
    Join Date
    Mar 2009
    Location
    Calgary, Alberta
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Nope. This isn't working at all... worse than before unfortunately.

    The cookie isn't set right. By the way does the cookie have to be in the root directory? because right now its sitting in /javascript/cookie.js

  10. #10
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    It shouldn't be a problem. After thinking about it a little bit, I think it may have something to do with the expiration date being set on the cookie. I'm not 100% on this, but perhaps someone else may be able to shed some light on this. Sorry I can't be of more help at the moment, but I will try to figure this out and let you know if I do.

    Edit: ok, last try. Add the highlighted below to the following:

    Code:
    exdate.setDate(exdate.getDate()+30);
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  11. The Following User Says Thank You to thetestingsite For This Useful Post:

    mrmozer (04-19-2009)

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
  •