...

View Full Version : date image switcher



ubik
12-25-2005, 08:04 PM
could someone please tell me whats wrong with this script? i have a feeling it has to do with the getElementById ? im still a new-b so any help appreciated, thanks:



<html>
<head>

<script language="javascript">
today = new Date();
christmas = new Date(2005,11,25);

function switcher() {
if (today==christmas) {
document.getElementById("logo").src="logo1.jpg";
}
}
</script>
</head>

<body onload="switcher()">
<center>
<img id="logo" src="neutral.jpg">

</body>
</html>

vwphillips
12-25-2005, 10:34 PM
<html>
<head>

<script language="javascript">
today = new Date();
today = new Date(today.getFullYear(),today.getMonth(),today.getDate()); // else only true foe a milli sec
christmas = new Date(2005,11,25);

function switcher() {
if (today-christmas==0) {
alert('its christmas')
document.getElementById("logo").src="logo1.jpg";
}
}
</script>
</head>

<body onload="switcher()">
<center>
<img id="logo" src="neutral.jpg">

</body>
</html>

ubik
12-26-2005, 12:26 AM
thank you very much vwphillips, it works very nicely. I would however like to further my knowledge of javascript, and if someone wouldn't mind as to explain to me what exactly is the purpose of the red hyphen in the following line of code, and why are they equaling 0?:



if (today-christmas==0) {



thanks.

vwphillips
12-26-2005, 09:35 AM
if (today-christmas==0) they are the same

ubik
12-27-2005, 02:19 AM
oh thats freakin awesome!, lol. it just came to me now that your subtracting the dates in milliseconds. wow that impressed me :D :thumbsup: thanks again.

ubik
12-27-2005, 08:57 AM
I only have one more question..

How is it that:

if (today==christmas)
is not true

but:

if (today-christmas==0)
is true

how is it that they are not equal to each other, yet when they are subtracted from each other they equal 0 ?

vwphillips
12-27-2005, 01:07 PM
they have the same value but they are not the same object

could have used

if (today.getTime()==christmas.getTime())

but requires more typing

the

'-' does this by auto typing (uses the metod appropriate to '-')

ubik
12-27-2005, 09:01 PM
oh, ok. thanks again.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum