What's new

Need Help with Javascript

soccerboi

New member
I am making a custom homepage, and I want to know how to make it show a new comic for the day. The image for today is called 050902.gif. Tommarows, 050903.gif. Is there a way to have the computer load todays image everyday? I'm pretty sure this would involve using javascript, but I am not sure.

So basically what I want is for the computer to find out the date, and make the filename according to the date.
 

smcd

Active member
There's probably a MUCH better way and I've not tested it thoroughly but it seems to work.

Code:
<html>

<head>

<script>
function findcomic()
{
  var d = new Date();
  var comic = d.getFullYear().toString();
  comic = comic.substr(2,2);
  if(d.getMonth() < 10)
  {
    comic+="0";
  }
  comic+=(d.getMonth()+1).toString();
  if(d.getDate() < 10)
  {
    comic += "0";
  }
  comic+=d.getDate().toString();
  comic+=".gif";
  document['todaycomic'].src = comic;
}
</script>

</head>

<body onload="findcomic()">
<img src="nothing_here.gif" name="todaycomic">
</body>

</html>
 
OP
S

soccerboi

New member
thank you for the reply.

But how do I get the variables into the filename where you put "nothing_here.gif"
 

zenogais

New member
the line

Code:
document['todaycomic'].src = comic;

replaces whatever is in the src attribute the img tag with the name attribute value set to 'todaycomic' with the source of an actual comic strip.
 

zAlbee

Keeper of The Iron Tail
it's preferable to use

Code:
document.images['todaycomic'].src = comic;

You probably also don't want to call it onLoad. If you do that, the script doesn't run until the whole page is finished loading. If there is a lot of stuff on the page, there can be a large delay until the image URL is finally changed.

also, weird behaviour: firefox works with the page initially, but if you hit reload, it just keeps trying to load the non-existant "nothing_here.gif"... and never runs the function. hm.
 

smcd

Active member
I said I didn't test it thoroughly, nor did I claim to know JS and HTML stuff well ;)
 

Doomulation

?????????????????????????
Or, you could also put src to = "" so that it actually will not display a picture and then call the script in onload to make sure the page html is loaded before. And then you can always use a little loading script that displays instead of the picture until it's loaded :p

And btw, firefox doesn't like <script>. You should type <script language="javascript">.
 

zenogais

New member
You could also do it another way:

Code:
<html>
<head>
  ...
</head>
<body>
  <img src="theComic.gif" alt="Comic Image" name="theComic" />
  
  <!-- Replace faux-image with actual comic -->
  <script type="text/javascript" langauge="javascript">
    //...
 
    // Daily Comic Picker Code Here

    //...
    document.images['theComic'].src = todaysComicURI;
  </script>
</body>
</html>

This will avoid the need for onload.
 

BGNG

New member
Why not generate the IMG tag with the JavaScript?

Code:
<HTML><Body>
Hi! This is a web page!<BR><BR>

Check out this comic:<BR>

<Script Language="JavaScript">
var TodayString;
// Get that day code to fill TodayString with today's date
document.write('<IMG SRC="' + TodayString + '.gif">');
</Script>
<BR><BR>

Cool, huh?
</Body></HTML>
Notice how the JavaScript is inline with the rest of the web page.
 

Top