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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Check cookie value then display image

    Hello everyone. I am trying to put together a browser based game which uses javascript and cookies, and am looking for help with one of it's functions.

    I need a script which checks a cookie value, then displays a certain image, depending on what that value is. So, if the value is 50, it displays one, if it's 60 it display another one etc.

    What script would I use?

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,772
    Thanks
    55
    Thanked 518 Times in 515 Posts
    a simple example - googling will get you lots of info about cookies...

    Code:
    <html>
    <head>
    <script type="text/javascript">
    
    
    
    function getCookie(c_name)
    {
    var i,x,y,ARRcookies=document.cookie.split(";");
    for (i=0;i<ARRcookies.length;i++)
      {
      x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
      y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
      x=x.replace(/^\s+|\s+$/g,"");
      if (x==c_name)
        {
        return unescape(y);
        }
      }
    }
    
    function setCookie(c_name,value,exdays)
    {
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
    document.cookie=c_name + "=" + c_value;
    }
    
    function checkCookie()
    {
    var number=getCookie("number");
    if (number!=null && number!="")
      {
      showPic(number)
      }
    else
      {
      number=prompt("Please enter a number:","");
      if (number!=null && number!="")
        {
        setCookie("number",number,365);
    	showPic(number)
        }
      }
    }
    
    function deleteCookie(the_cookie) {
        document.cookie = encodeURIComponent(the_cookie) + "=deleted; expires=" + new Date(0).toUTCString();
    }
    
    function showPic(thenum){
    document.getElementById("thepic").src=thenum+".jpg"
     } 
    </script>
    </head>
    <body onload="checkCookie()">
    <img src="" id="thepic" onerror="alert('we have no image on file with that name'); deleteCookie('number'); checkCookie()">
    </body>
    </html>
    Last edited by xelawho; 01-14-2012 at 03:29 PM.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Don't forget that users may (and often do) delete their cookies.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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