Cookies

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wangers16
    New Member
    • Jul 2007
    • 57

    Cookies

    Hi,

    I have the following code for use in a cookie script that can change the stylesheet depending upon what the visitor has decided on, here it is

    Code:
    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 "vista"
    }
    I was wondering how I could get this to work through different browser sessions because at the moment it is reverting back to the original everytime the page is loaded

    Thnks in advance
  • Logician
    New Member
    • Feb 2007
    • 210

    #2
    Originally posted by wangers16
    I was wondering how I could get this to work through different browser sessions because at the moment it is reverting back to the original everytime the page is loaded
    That is determined when the cookie is set. If you want a session cookie, don't specify a duration.

    You're welcome in advance.

    Comment

    • wangers16
      New Member
      • Jul 2007
      • 57

      #3
      I have set a duration, however it is still only working with browser sessions

      Here is the code that set's the cookie, if that is any help

      Code:
      function createCookie(c_name,value,expiredays)
      {
      var exdate=new Date();
      exdate.setDate(exdate.getDate()+expiredays);
      document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toGMTString());
      }
      and this is the button used to activate the command

      Code:
      <input type="button" value="Login" onClick="createCookie('style','old','1000000');">

      Comment

      • Logician
        New Member
        • Feb 2007
        • 210

        #4
        Originally posted by wangers16
        This is the button used to activate the command

        Code:
        <input type="button" value="Login" onClick="createCookie('style','old','1000000');">
        This suggests that when someone clicks a 'login' button, you're creating a fixed-value cookie, whereas you said initially that you're storing a user preference. I think you need to clarify exactly what happens and what you expect.

        Comment

        • wangers16
          New Member
          • Jul 2007
          • 57

          #5
          Well the script is supposed write a user preference value to a cookie and that cookie is then later read by the computer and writes out a predefined stylesheet onto the site.

          Now this does happen but it only happens per a user session only, whereas I need it to be able to be read at any browser session without the need of having to choose which style to use every time.

          Comment

          • Logician
            New Member
            • Feb 2007
            • 210

            #6
            Originally posted by wangers16
            Well the script is supposed write a user preference value to a cookie and that cookie is then later read by the computer and writes out a predefined stylesheet onto the site.

            Now this does happen but it only happens per a user session only, whereas I need it to be able to be read at any browser session without the need of having to choose which style to use every time.
            You have shown no code that reads a user preference and writes it to a cookie.
            You showed a Login button setting some cookie to a fixed value. If that's the same cookie, then obviously any value it stores is being reset whenever that button is clicked.

            Comment

            Working...