471,887 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,887 software developers and data experts.

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!
Aug 11 '08 #1
4 958
608 512MB
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.getElementById('picPosition').innerHTML = "Picture during Day Time";
document.getElementById('picPosition').innerHTML = "Picture during Night Time";

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

Ramanan Kalirajan
Aug 11 '08 #2
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.
Aug 11 '08 #3
608 512MB
Hi this is a working example instead of the div you replace it image tag and set the source in the condition part.

<body onload="doThis()">
<div id="myDiv">
<script type="text/javascript">
function doThis()
var toDate = new Date();
var hours = toDate.getHours();
// alert(hours);
document.getElementById('myDiv').innerHTML="Beauti ful Day";
document.getElementById('myDiv').innerHTML="Beauti ful Evening Time";

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

Ramanan Kalirajan
Aug 12 '08 #4
16,027 Expert Mod 8TB
To set the source of the image, set its src property, e.g.
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("myImg").src = "daytime.png";
You could also set this using server-side code, but then that would be based on server-time rather than the client.
Aug 12 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by entoone | last post: by
1 post views Thread by Jamie Meredith | last post: by
5 posts views Thread by Charax | last post: by
6 posts views Thread by randy1200 | last post: by
1 post views Thread by Rabel | last post: by
1 post views Thread by icepick72 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.