Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11

Thread: Cookie problems

  1. #1
    New to the CF scene
    Join Date
    Aug 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cookie problems

    The following is the script and selection menu I am using to let a user choose a background for a table:

    <script>
    function changeTableBg(selectObject){
    var imgName=selectObject[selectObject.selectedIndex].value;
    document.getElementById("myTable").setAttribute("background",imgName)
    }
    </script>


    and the actual selection menu:

    <select onChange="changeTableBg(this)" >
    <option value="bg2.jpg">Select a color below</option>
    <option value="bg2.jpg">New Blue</option>
    <option value="bg1.jpg">Feeling Green?</option>
    </select>


    What i need help with is the code to set and retrieve a cookie that will remember what background the user chose for future visits. Everytime I attempt making a cookie, all I end up with is the cookie remembering the option but not applying the effect (ex: the cookie remembers the "Feelign Green?" text but doesn't actually set the "bg1.jpg" image as the background).

    Can someone help me?

  • #2
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Code:
    function changeTableBg(selectObject){
      var imgName=selectObject.options[selectObject.selectedIndex].value;
      document.getElementById("myTable").setAttribute("background",imgName)
      document.cookie = 'background=' + imgName;
    }
    window.onload = select_bg;
    function select_bg() {
      if(document.cookie) {
        var img = document.cookie.split('=');
        document.getElementById("myTable").setAttribute("background", img[1]);
      }
    }
    dumpfi

  • #3
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Heh Dumpfi;

    I thought I should point out that without setting and expiry date for the cookie, the cookie will be session only and will not be available for future visits....

    Here is my offering... Depending upon your host... You may need to uncomment the domain and path line...

    Code:
    <script type="text/javascript">
    <!--//
      function setCookie(imgName){
       var date = new Date();
           date.setTime(date.getTime() + 365*24*60*60*1000);
           var expString = '; expires = '+date.toGMTString();
    //         expString+= '; domain = '+location.host+'; path=\/;';
           document.cookie = 'imgName='+imgName+expString;
           document.getElementById("myTable").setAttribute("background",imgName)
      }
    
      function chkCookie(){
        if(/^imgName=(\w+)$/.test(document.cookie)){
           document.getElementById("myTable").setAttribute("background",RegExp.$1)
        }  else return;     
      }
     //-->
    </script>
    </head>
    
    <body onload="chkCookie()">
    <form>
    <select onChange="setCookie(this.value)" >
    <option value="bg2.jpg">Select a color below</option>
    <option value="bg2.jpg">New Blue</option>
    <option value="bg1.jpg">Feeling Green?</option>
    </select>
    </form>
    .....Willy
    Last edited by Willy Duitt; 08-29-2004 at 11:13 AM.

  • #4
    New to the CF scene
    Join Date
    Aug 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I greatly appreciate your help!

  • #5
    New to the CF scene
    Join Date
    Aug 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I just tested the code and it has the same problem as my attempt, it doesn't apply the background. All it remembers is the option chosen but doesn't actually apply the background.

  • #6
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Then you are doing something wrong...
    You will need to provide a link or at least some code....

  • #7
    New to the CF scene
    Join Date
    Aug 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here is the basic coding for my website. Perhaps it might give you a better way of helping me figure this out.

    <body>
    <script>
    function changeTableBg(selectObject){
    var imgName=selectObject[selectObject.selectedIndex].value;
    document.getElementById("myTable").setAttribute("background",imgName)
    }
    </script>

    <br />
    <br />

    <div align="center">
    <center><table BORDER=0 CELLSPACING=0 CELLPADDING=0 COLS=1 WIDTH="760" >
    <tr VALIGN=TOP>
    <td VALIGN=TOP>
    <p align="center">
    <br>&nbsp;
    <select onChange="changeTableBg(this)" >
    <option value="bg2.jpg">Select a color below</option>
    <option value="bg2.jpg">New Blue</option>
    <option value="bg1.jpg">Feeling Green?</option>
    </select>

    </p>
    </td>
    </tr>

    </table></center>
    </div>
    <div align="center">
    <center>

    <table background="" width="760" id="myTable" cellspacing="0" cellpadding="12">
    <tr>
    <td width="50%" valign="top"><font size="-1" face="Verdana">Column 1
    with lots of text</font>
    </td>
    <td valign="top" width="50%">
    column 2 with more text</td>
    </tr>
    </table>
    </center>
    </div>
    </body>


    The thing that really bothers me is that the cookie seems to work, it just doesn't apply or activate the background colors. If it can remember the choice, why can't it set the background?

  • #8
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What's that??

    Where is the cookie script??
    How can someone tell what you are doing wrong if you do not show us the script you tried to implement and which you say doesn't work??

    .....Willy

    Edit: BTW:....
    Please read the Posting Guidelines and use [code].....CODE HERE.....[/code] tags when posting code....

  • #9
    New to the CF scene
    Join Date
    Aug 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here's the code for the entire web page (the basics) with the cookie code in place.

    Code:
    <!-- HERE'S THE COOKIE CODING (IN THE HEADER)--!>
    <script type="text/javascript">
    <!--//
      function setCookie(imgName){
       var date = new Date();
           date.setTime(date.getTime() + 365*24*60*60*1000);
           var expString = '; expires = '+date.toGMTString();
          expString+= '; domain = '+location.host+'; path=\/;';
           document.cookie = 'imgName='+imgName+expString;
           document.getElementById("myTable").setAttribute("background",imgName)
      }
    
      function chkCookie(){
        if(/^imgName=(\w+)$/.test(document.cookie)){
           document.getElementById("myTable").setAttribute("background",RegExp.$1)
        }  else return;     
      }
     //-->
    </script>
    </head>
    
    <!-- THE CODING TO CHECK FOR A COOKIE --!>
    
    
    <body onload="chkCookie()" bgcolor="#000000" text="#FFFFFF">
    
    <br />
    <br />
    
    <div align="center">
      <center><table BORDER=0 CELLSPACING=0 CELLPADDING=0 COLS=1 WIDTH="760" >
    <tr VALIGN=TOP>
    <td VALIGN=TOP>
      <p align="center">
    <br><em>The logo is here</em>
      
    <!--THE FORM CODE IS HERE, YOU CAN SEE THAT THE FORM IS IN A TABLE ON TOP OF ANOTHER TABLE (WHICH IS THE ONE THAT CHANGES BACKGROUND)--!>
    
    <form><select onChange="setCookie(this.value)">
    <option value="bg2.jpg">Select a color below</option>
    <option value="bg2.jpg">New Blue</option>
    <option value="bg1.jpg">Feeling Green?</option>
    </select>
    </form>
    
    </td>
    </tr>
    
    </table></center>
    </div>
    <div align="center">
      <center>
    
    <!-- THIS IS WHERE THE BACKGROUND IS SUPPOSED TO CHANGE. THE CODE WORKS BUT THE COOKIE FAILS TO ACTUALLY SET THE REMEMBERED COLOR --!>
    
    
    <table background="" width="760" id="myTable" cellspacing="0" cellpadding="12">
    	<tr>
    		<td width="50%" valign="top"><font size="-1" face="Verdana">This is the
              basic outline of the site</font>
            </td>
    		<td valign="top" width="50%">
      The cookie code is in place but doesn't work!</td>
    	</tr>
    </table>
      </center>
    </div>
    </body>
    I hope this helps a little more. I tried placing the script in many different places but always end up with the same result. I know we're close, but I must be overlooking something.

  • #10
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try changing the regular expression to: /^imgName=(\w+\.\w+)$/ or if your image paths also include sub directories... Try using Dumpfi's split method rather than a regular expression....

    .....Willy

  • #11
    New to the CF scene
    Join Date
    Aug 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    that did the trick, thanks!


  •  

    Posting Permissions

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