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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Mar 2004
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Hello! Need help keeping cookies in script!



    Hello all! I have this script that changes the color of the page, but I cant get the cookie to change the color to what the user selected when they return to the page. The cookie gets put in the cookie file, but thats it. Do I need another function besides setCookie to keep the cookie there? Thanks for any help! Norman.

    <html>
    <head>

    <title>Color change cookies</title>

    <script>
    function setCookie()
    {
    var expiresDate = new Date();

    expiresDate.setFullYear(expiresDate.getFullYear() + 1);
    document.cookie = encodeURI("setCookie="
    + document.colorchoice.selection.value)+ "; expires = "+ expiresDate.toUTCString();

    alert("Your background color has been saved for your next visit to this page");


    }


    function colorit() {
    var Color = parseInt(document.colorchoice.selection.selectedIndex)
    changeColor(Color)

    }



    var colorlist = new Array("red","blue","yellow")
    function changeColor(color) {
    document.bgColor = colorlist[color]
    document.colorchoice.value = colorlist[color]
    }



    </script>
    </head>
    <body>
    <form name="colorchoice">
    <p> <h2>Click button to pick color.</h2></p>
    <SELECT NAME="selection" value="selectedIndex">
    <OPTION>Red
    <OPTION>Blue
    <OPTION>Yellow
    </SELECT>
    <INPUT TYPE="button" NAME="Change_Color" VALUE="Change Color" onClick="colorit(); setCookie();">


    </form>


    </body>
    </html>

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    JPM
    JPM is offline
    Regular Coder
    Join Date
    Mar 2004
    Location
    Norway
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    expiresDate.toUTCString();
    Shouldn't that be ....'toGMTString()' ?
    <JPM />

  • #4
    New to the CF scene
    Join Date
    Mar 2004
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks A1ien51 and JPM! I changed the code to GMTS, but still no luck. I saw the code from the link, but would like to keep my code if possible and use the button to set the cookie. I was looking at the coded link, but could not figure out how to change it for my code to work. Sorry, It gets pretty confusing and I am fairly new to javascript. Isn't there a function I can add to keep these cookies, and just change it to the function and names I am using? This seems like a basic script I wrote and I am surprised this seems so difficult to just keep a cookie. Thanks again for the help guys! Norman.

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    [object Date].toUTCString

    It should be UTC.




    Hmm, have you checked the JavaScript Console? You're trying to set the value of the form itself, which is plainly wrong, in the following passage
    Quote Originally Posted by normpeterson
    Code:
    function changeColor(color) {
    document.bgColor = colorlist[color]
    document.colorchoice.value = colorlist[color]
    }
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  

    Posting Permissions

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