Forum Moderators: open

Message Too Old, No Replies

"Image a day" script not working

However there doesn't seem to be any bug...

         

Me_Uzik

6:00 am on Feb 7, 2004 (gmt 0)

10+ Year Member



Hi,
I have a script to make a different image appear everyday on a web page;
There doesn't seem to be any bug, however the image won't load...
Here's the script, I hope someone can see what's wrong:

in the head of the html:

<script type="text/javascript">
var now = new Date();
var month = new String(now.getMonth());
var day = new String(now.getDate());
if (month.length!= 2) {month = "0" + month;}
if (day.length!= 2) {day = "0" + day;}
var img = month + "-" + day;
</script>

and in the body of the html:

<script type="text/javascript">
document.write("<img src='http://www.example.com/bg/' + img + '.jpg' alt=''>");
</script>

If you go to the directory [example.com...] you will see images.
The script should be calling for 02-07.jpg in this directory, (since we are february 7th) and the image is there, but it won't load...

I hope someone can see what's wrong...
Thanks,

Me-Uzik

[edited by: korkus2000 at 12:44 am (utc) on Feb. 8, 2004]
[edit reason] examplified URLs [/edit]

isitreal

5:58 pm on Feb 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



A few subtle mistakes made this not work, you mixed up ' and " in the document write, also the month array begins at 0, not 1, and you were converting the month and day to a string, which is not necessary in this case with javascript, also you need the month to be an integer so you can add one to it. I tested this in ie, opera, netscape 4, and mozilla and it works fine. The alert method is a good way to debug code, it lets you know what you actually have at each step.

The below version works now:

<script type="text/javascript">
var now = new Date();
var month = now.getMonth();
month = month + 1;
var day = now.getDate());
if (month.length!= 2) {month = "0" + month;}
if (day.length!= 2) {day = "0" + day;}
alert (day + '\n' + month);
var img = month + "-" + day;
alert (img);
</script>

<script type="text/javascript">
document.write("<img src='" + img + ".jpg' alt=''>");
</script>

dcrombie

9:39 pm on Feb 7, 2004 (gmt 0)



I know this is off-topic, but if you've got PHP:

<IMG src="<?= date ("m-d")?>.jpg" alt="">

Me_Uzik

12:25 am on Feb 8, 2004 (gmt 0)

10+ Year Member



Hi,
thank you so much isitreal!
There was a small mistake in your script though, but the debugger told me:
var day = now.getDate());
there is a ) too many.
I erased it and now it works!

dcrombie,
I never used PHP, however it could be a good idea.
Thanks!

Me-Uzik

isitreal

12:36 am on Feb 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Sorry about the typo, glad it's working for you.

The main problem with developing in PHP is having to set up your own home apache webserver with PHP, that's not always super easy to do, but it is much more reliable in terms of being sure your code runs as consistently as possible, since javascript can be turned off, and sometimes is.

Me_Uzik

5:21 am on Feb 10, 2004 (gmt 0)

10+ Year Member



Hey,
there seems to be a problem now...
The image of the day is not showing anymore, and there's a 02-10.jpg image in the same directory as yesterday's image, so it should be showing...
Maybe because the day changed to a 2 numbers day?...
(The 9th being only a 1 number day, although the script says to put a 0 before it... Would there be a bug when the day reaches 2 numbers?)
You can see for yourself here:
[bigbeautifulbackgrounds.com...]
I hope someone can find what's wrong again...
Thanks,

Me-Uzik

Purple Martin

5:46 am on Feb 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Put the alerts back in, and you'll be able to see for yourself if the filename is being formed properly.

Me_Uzik

6:56 am on Feb 10, 2004 (gmt 0)

10+ Year Member



Thanks Purple Martin;
I tried the alert and yes, it tries to get the day called "010"...
It will probably try to get the month "010" also when it will get there...
Unfortunately I didn't do the script myself, so I don't really know what to change...
I don't have much knowledge of programming a javascript.
If some of you know what should be changed or added it would be very helpful and appreciated. :)

Thanks,

Me-Uzik

isitreal

5:50 pm on Feb 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Sorry, you did need this to be converted to a string, here's the correct version I think:

<script type="text/javascript">
var now = new Date();
var month = now.getMonth() + 1;
var day = now.getDate();
month = new String( month );
day = new String( day );
if (month.length < 2) {month = "0" + month;}
if (day.length < 2) {day = "0" + day;}
alert (day + '\n' + month);
var img = month + "-" + day;
alert (img);
</script>
<script type="text/javascript">
document.write("<img src='" + img + ".jpg' alt=''>");
</script>

Me_Uzik

9:20 pm on Feb 10, 2004 (gmt 0)

10+ Year Member



Thanks isitreal!
It works now.

Thanks again to all of you. :)

Me-Uzik