469,344 Members | 6,152 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,344 developers. It's quick & easy.

If then statement for graphic changes

2
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 928
RamananKalirajan
607 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";
else
document.getElementById('picPosition').innerHTML = "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
Aug 11 '08 #2
darmot
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
RamananKalirajan
607 512MB
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.getElementById('myDiv').innerHTML="Beauti ful Day";
else
document.getElementById('myDiv').innerHTML="Beauti ful Evening Time";
}
</script>[/HTML]

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

Regards
Ramanan Kalirajan
Aug 12 '08 #4
acoder
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
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.