If then statement for graphic changes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • darmot
    New Member
    • Aug 2008
    • 2

    If then statement for graphic changes

    I am wondering if it would be possible to set the display of a grahic on a website to change depending on the time of day it is. Say I want to have a night and day photgraph of a building on the website. Now can I have the page load with the proper picture relating to the time of day using if-then statements or would I need to try soemthing else if it is possible at all? Thank you!
  • RamananKalirajan
    Contributor
    • Mar 2008
    • 608

    #2
    Ya surely you can do that with the help of the Date reference. I am just giving u the sample idea. In the window.onload have a function like this

    [HTML]function checkDay()
    {
    var todate = new Date();
    var hour = todate.getHours ()
    if(Hours<17) //upto Evening five
    document.getEle mentById('picPo sition').innerH TML = "Picture during Day Time";
    else
    document.getEle mentById('picPo sition').innerH TML = "Picture during Night Time";

    }[/HTML]
    See this is a sample code, just try yours if any probs post back it. I will try to help u out

    Regards
    Ramanan Kalirajan

    Comment

    • darmot
      New Member
      • Aug 2008
      • 2

      #3
      I guess I am more lost then i realized. I copied this and pasted it into my file yet nothing happened. I amde the changes I thought I needed to make such as the file names, and still nothing. Am I missing something? Perhaps this is more complex of an element then i was prepared for. If anyone has the time out there and can break it down to being very simple then maybe I can make it work on my site. Thank you very much for any efforts.

      Comment

      • RamananKalirajan
        Contributor
        • Mar 2008
        • 608

        #4
        Hi this is a working example instead of the div you replace it image tag and set the source in the condition part.

        [HTML]<html>
        <body onload="doThis( )">
        <div id="myDiv">
        </div>
        </body>
        </html>
        <script type="text/javascript">
        function doThis()
        {
        var toDate = new Date();
        var hours = toDate.getHours ();
        // alert(hours);
        if(hours<18)
        document.getEle mentById('myDiv ').innerHTML="B eautiful Day";
        else
        document.getEle mentById('myDiv ').innerHTML="B eautiful Evening Time";
        }
        </script>[/HTML]

        Still any doubts post it back I will try to help u out.

        Regards
        Ramanan Kalirajan

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          To set the source of the image, set its src property, e.g.
          [code=javascript]document.getEle mentById("myImg ").src = "daytime.pn g";[/code]
          You could also set this using server-side code, but then that would be based on server-time rather than the client.

          Comment

          Working...