By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,480 Members | 1,808 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,480 IT Pros & Developers. It's quick & easy.

If then statement for graphic changes

P: 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
Share this Question
Share on Google+
4 Replies

P: 607
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

P: 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

P: 607
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

Expert Mod 15k+
P: 16,027
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.