Forum Moderators: open

Message Too Old, No Replies

Need Images to display based on Season - HELP!

         

eroyer

4:29 am on Oct 6, 2007 (gmt 0)

10+ Year Member



Hi

I am trying to use the following code to establish what the season is and to assign the header image to it. But I am not sure how to get it to work. I am also not sure how to call out the code in the Body of the HTML so that it will load the correct image.

If anyone can help I would appreciate it

<SCRIPT LANGUAGE="JavaScript">
var today = new Date(){
var month = today.getmonth()+1;
var day = today.getDate();
var season ='images/header.jpg';
if (month<=11 && day <21)
season ='images/headerfall.jpg';
if (month<=8 && day <21)
season ='images/headersummer.jpg';
if (month<=5 && day <21)
season ='images/headerspring.jpg';
if (month<=2 && day <21)¦¦(month=11 && day>20))
season ='images/headerwinter.jpg';
window.location.href = season;
}
// End -->
</SCRIPT>

Thank you
Erick

Dabrowski

1:00 pm on Oct 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nice try Erick. You just need to modify this code to change the image location, I imaging at the moment it's redirecting the entire page to your image.

Here's a quickie, I haven't error checked your existing code, it look ok, apart from a couple of superfluous {}'s.


<html>

<head>
<script>
function setHeader() {

var today = new Date();
var month = today.getmonth()+1;
var day = today.getDate();
var season ='images/header.jpg';
var headerImage = document.getElementById( "header");

if (month<=11 && day <21) season ='images/headerfall.jpg';
if (month<=8 && day <21) season ='images/headersummer.jpg';
if (month<=5 && day <21) season ='images/headerspring.jpg';
if (month<=2 && day <21)¦¦(month=11 && day>20))
season ='images/headerwinter.jpg';

headerImage.src = season;
}
</script>
</head>

<body>
...
<img id='header' src='images/header.jpg'>
<script> setHeader(); </script>
...
</body>

</html>

ok, here's what I did. First put your code into a function inside the header. Second, I've added an ID to your <img> tag, that also contains the default image to start with. As soon as the browser gets to the <script> after the <img>, it calls your function. The function then looks up the image on the page by it's ID, and sets the .src property to your image URL.

See how you get on with that.