Total newb JavaScript help; displaying images based on hour of the day?
6 replies, posted
Hi guys, I'm an idiot and I'm trying to code a bit of stuff into a site I'm working on where depending on what hour of the day you look at it, a different image appears.
I'm using an external .js, and I just ripped and altered this code from a tutorial:
[code]// JavaScript Document
function eyeMe() {
var now = new Date;
var timeNow = now.getHours();
var img = null;
if (timeNow == 0) {
img = '0.jpg';
} else if (timeNow == 1) {
img = '1.jpg';
} else if (timeNow == 2) {
img = '2.jpg';
} else if (timeNow == 3) {
img = '3.jpg';
} else if (timeNow == 4) {
img = '4.jpg';
} else if (timeNow == 5) {
img = '5.jpg';
} else if (timeNow == 6) {
img = '6.jpg';
} else if (timeNow == 7) {
img = '7.jpg';
} else if (timeNow == 8) {
img = '8.jpg';
} else if (timeNow == 9) {
img = '9.jpg';
} else if (timeNow == 10) {
img = '10.jpg';
} else if (timeNow == 11) {
img = '11.jpg';
} else if (timeNow == 12) {
img = '12.jpg';
} else if (timeNow == 13) {
img = '13.jpg';
} else if (timeNow == 14) {
img = '14.jpg';
} else if (timeNow == 15) {
img = '15.jpg';
} else if (timeNow == 16) {
img = '16.jpg';
} else if (timeNow == 17) {
img = '17.jpg';
} else if (timeNow == 18) {
img = '18.jpg';
} else if (timeNow == 19) {
img = '19.jpg';
} else if (timeNow == 20) {
img = '20.jpg';
} else if (timeNow == 21) {
img = '21.jpg';
} else if (timeNow == 22) {
img = '22.jpg';
} else {
img = '23.jpg';
}
document.write('<img src="images/fw/eyes/'+ img +'" />');
}[/code]
I KNOW it's super inefficient. I tried this, but it didn't work, either:
[code]function eyeMe() {
var now = new Date;
var timeNow = now.getHours();
document.write('<img src="images/fw/eyes/'+ timeNow +'".jpg" />');
}
[/code]
That [i]should[/i] work, shouldn't it? The first code was merely made out of desperation. And I have a folder in the proper directory, named 0.jpg through 23.jpg.
Here's what I'm using to reference the external .js:
[code]<SCRIPT type="txt/javascript" SRC="index.js"></SCRIPT>[/code]
This is how I'm calling the script:
[code]
<div id="holder">
<div id="topbar">
<div id="logo"><script type="txt/javascript">
eyeMe();
</script>
</div>
</div>
</div>
[/code]
I honestly can't figure out why it wouldn't work. When I load the page, there's simply nothing in the "logo" DIV. Any suggestions?
1. HTML tags should be lower case (this is just me trying you to conform to standards)
2. text/javascript not txt/javascript
See if doing 2 gets you anywhere.
[QUOTE=Scyze;20773605]Hi guys, I'm an idiot[/quote]
Nah you just fail hard at javascript
[QUOTE=Scyze;20773605]
[code]
document.write('<img src="images/fw/eyes/'+ timeNow +'".jpg" />');
}
[/code]
[/QUOTE]
Spot the mother fucking problem.
Hint: that produces: <img src="images/fw/eyes/11".jpg" />
[editline]09:37AM[/editline]
[QUOTE=a2h;20774902]1. HTML tags should be lower case (this is just me trying you to conform to standards)
2. text/javascript not txt/javascript
See if doing 2 gets you anywhere.[/QUOTE]
Also, what he said.
[QUOTE=possibly_vladh;20776107]Nah you just fail hard at javascript
Spot the mother fucking problem.
Hint: that produces: <img src="images/fw/eyes/11".jpg" />
[editline]09:37AM[/editline]
Also, what he said.[/QUOTE]
Yeah! You're right, I fail hard! Thanks for the spottin'. It's all good now. :love:
[QUOTE=a2h;20774902]1. HTML tags should be lower case (this is just me trying you to conform to standards)
2. text/javascript not txt/javascript
See if doing 2 gets you anywhere.[/QUOTE]
I also took this advice, too.
That first code snippet is TDWTF worthy
They put that into tutorials?
[QUOTE=DrTaxi;20780416]They put that into tutorials?[/QUOTE]
Hahahaha, oh my science. I just re-read the OP
Sorry, you need to Log In to post a reply to this thread.