Current location: Hot Scripts Forums » General Web Coding » JavaScript » trouble with 'image of the day' code


trouble with 'image of the day' code

Reply
  #1 (permalink)  
Old 10-05-05, 06:46 AM
mrblue mrblue is offline
New Member
 
Join Date: Oct 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
trouble with 'image of the day' code

hello,
i found a script to display a daily rotated image which i was hoping to use on my site. after copying the code into a page and setting up the the images in the format dd_mm.jpg, i can't seem to get it to work. the page displays a broken image link. the code for the page is below:

Code:
<html>
<head>
<title>Today's Found Object...</title>
<script language="javascript" type="text/javascript">
function loadImage()
{
        var today = new Date();
        var imgString = today.getDate()+"_"+today.getMonth()+".jpg";
        document.getElementById("foundobject").src = imgString;
}
</script>
</head>
<body bgcolor="#CCCCCC" onLoad="loadImage();">
<img src="" id="foundobject" alt="Today's Found Object...">
</body>
</html>
any ideas why this isn't working? thanks.
Reply With Quote
  #2 (permalink)  
Old 10-05-05, 08:17 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
are you sure of the path? the images are all located in the same dir where this page is located?
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #3 (permalink)  
Old 10-06-05, 06:59 AM
mrblue mrblue is offline
New Member
 
Join Date: Oct 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by NeverMind
are you sure of the path? the images are all located in the same dir where this page is located?
the images and html are in a folder marked "foundobject" with the images marked "dd_mm.jpg", for instance today's is 06_10.jpg.

just checked to see if today's works, and it doesn't appear to.
Reply With Quote
  #4 (permalink)  
Old 10-06-05, 09:16 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
the problem is that the returned value of getDate() and getMonth() are without leading zeros!
so, imgString today will contain somthing like: 6_9.jpg (note that the month offset starts from 0 instead of 1)

so here is the script after modifications:
Code:
<html>
<head>
<title>Today's Found Object...</title>
<script language="javascript" type="text/javascript">
function loadImage()
{
        var today = new Date();
        var day = today.getDate();
        var month = today.getMonth();

        if (day < 10)
            day = "0" + day;

        month++;
        if (month < 10)
            month = "0" + month;

        var imgString = day + "_" + month +".jpg";
        document.getElementById("foundobject").src = imgString;
}
</script>
</head>
<body bgcolor="#CCCCCC" onLoad="loadImage();">
<img src="" id="foundobject" alt="Today's Found Object...">
</body>
</html>
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #5 (permalink)  
Old 10-06-05, 10:15 AM
mrblue mrblue is offline
New Member
 
Join Date: Oct 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
ahh. i should have thought of that. many thanks for altering the code, it's working perfectly now!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Random Image Code reeksy PHP 11 02-14-05 07:04 PM
Web Image search script (altavista/google image search code?) downhillmatt2002 Script Requests 6 10-18-04 03:24 PM
how to draw an image inside a table? davidklonski HTML/XHTML/XML 2 07-06-04 10:27 AM
replacing input code with image. simone PHP 14 12-30-03 07:03 PM
perl/cgi newbie. hangman.cgi script adding html-like code on web and other trouble Arowana Perl 12 11-13-03 02:24 PM


All times are GMT -5. The time now is 10:54 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.