Results 1 to 4 of 4

Thread: Background Change script on click able to store choices from the user in cookies

  1. #1
    Join Date
    Jun 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Background Change script on click able to store choices from the user in cookies

    hi, im wondering how to modify the script to be able to store the decision of the user when they changes the background in the cookies

    Code:
    </script>
    <script language="JavaScript">
    <!--
    
    var backImage = new Array();
    
    backImage[0] = "back.jpg";
    backImage[1] = "back2.jpg";
    backImage[2] = "back.jpg";
    backImage[3] = "";
    
    function changeBGImage(whichImage){
    document.getElementById('background').src = backImage[whichImage];
    }
    
    //-->
    </script>

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,781
    Thanks
    2
    Thanked 415 Times in 409 Posts

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    
    <script language="JavaScript">
    <!--
    
    var backImage = new Array();
    
    backImage[0] = "http://www.vicsjavascripts.org.uk/StdImages/One.gif";
    backImage[1] = "http://www.vicsjavascripts.org.uk/StdImages/Two.gif";
    backImage[2] = "http://www.vicsjavascripts.org.uk/StdImages/Three.gif";
    backImage[3] = "";
    
    function changeBGImage(whichImage){
    document.getElementById('background').src = backImage[whichImage];
     zxcCreateCookie('nme',whichImage,1); // change 1 to the number of days presistance required
    
    }
    
    function zxcCreateCookie(nme,v,days){
     document.cookie=nme+'='+v+';expires='+(new Date(new Date().getTime()+days*86400000).toGMTString())+';path=/';
    }
    
    function zxcReadCookie(nme){
     nme+='=';
     var split = document.cookie.split(';');
     for(var z0=0;z0<split.length;z0++){
      var s=split[z0];
      while (s.charAt(0)==' ') s=s.substring(1,s.length);
      if (s.indexOf(nme)==0) return s.substring(nme.length,s.length);
     }
     return null;
    }
    
    function GetCookie(nme){
     changeBGImage(zxcReadCookie(nme)||0);
    }
    //-->
    </script>
    </head>
    <body onload="GetCookie('nme');">
    
    <img  id="background" src=" " /><br />
    <input type="button" name="" value="Image 0" onclick="changeBGImage(0);"/>
    <input type="button" name="" value="Image 1" onclick="changeBGImage(1);"/>
    <input type="button" name="" value="Image 2" onclick="changeBGImage(2);"/>
    
    </body>
    
    </html>

  3. #3
    Join Date
    Feb 2009
    Posts
    48
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default

    I think this is exactly what I'm trying to do in my post as well. Would you be able to apply what you did here over there? Thank you in advance...

    My post is here...
    http://www.dynamicdrive.com/forums/s...ad.php?t=46269

  4. #4
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,781
    Thanks
    2
    Thanked 415 Times in 409 Posts

    Default

    I think this is exactly what I'm trying to do in my post as well. Would you be able to apply what you did here over there? Thank you in advance...

    My post is here...
    http://www.dynamicdrive.com/forums/s...ad.php?t=46269
    answered on your post

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
  •