HighDots Forums  

Re: Picture of the day

Javascript JavaScript language (comp.lang.javascript)


Discuss Re: Picture of the day in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
trippy
 
Posts: n/a

Default Re: Picture of the day - 05-04-2006 , 02:41 AM






In article <1146718169.487623.56660 (AT) i39g2000cwa (DOT) googlegroups.com>,
CStewy took the hamburger, threw it on the grill, and I said "Oh wow"...

Quote:
I am trying to make a picture of the day script and I am having
problems displaying the image. Here is the code I have but I can not
get the image to show up. I have declared "image" as the picture file
name.

var months=new Array(13);
months[1]="1";
months[2]="2";
months[3]="3";
months[4]="4";
months[5]="5";
months[6]="6";
months[7]="7";
months[8]="8";
months[9]="9";
months[10]="10";
months[11]="11";
months[12]="12";
var time=new Date();
var lmonth=months[time.getMonth() + 1];
var date=time.getDate();
var image=lmonth +"-" +date+".jpg"
//document.write(lmonth + "-" );
//document.write("0" + date + "<BR>");
//document.write(image);
document.write("<Center><Img alt='Pic of The Day'> <img
src=/image/" + image + "></center>");
/script


Java isn't Javascript.

(comp.lang.javascript added)

--
trippy
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

NP: "Damn" -- Matchbox 20

"Now, technology's getting better all the time and that's fine,
but most of the time all you need is a stick of gum, a pocketknife,
and a smile."

-- Robert Redford "Spy Game"






Reply With Quote
  #2  
Old   
RobG
 
Posts: n/a

Default Re: Picture of the day - 05-04-2006 , 03:15 AM






trippy said on 04/05/2006 4:41 PM AEST:
Quote:
In article <1146718169.487623.56660 (AT) i39g2000cwa (DOT) googlegroups.com>,
CStewy took the hamburger, threw it on the grill, and I said "Oh wow"...


I am trying to make a picture of the day script and I am having
problems displaying the image. Here is the code I have but I can not
get the image to show up. I have declared "image" as the picture file
name.

var months=new Array(13);
months[1]="1";
months[2]="2";
months[3]="3";
months[4]="4";
months[5]="5";
months[6]="6";
months[7]="7";
months[8]="8";
months[9]="9";
months[10]="10";
months[11]="11";
months[12]="12";
A much more efficient way to declare the array is:

var months = ['1', '2', '3' ... '11', '12'];


But the array is completely unnecessary anyway.


Quote:
var time=new Date();
var lmonth=months[time.getMonth() + 1];
Say it's January, time.getMonth()+1 will return the number 1. When used
as an index to the months array, it will be type-converted to a string,
then used to look up the value of months[1], which is (drum roll...)
'1', which will be assigned to the variable lmonth.


Quote:
var date=time.getDate();
var image=lmonth +"-" +date+".jpg"
So what you could do is forget the array and use:

var image = (time.getMonth()+1) + '-' + time.getDate() + '.jpg';


Which, for say 15 January, will create an image name of '1-15.jpg'.


Quote:
document.write("<Center><Img alt='Pic of The Day'> <img
A src attribute is required, are you using the lack of a src attribute
to try and make the browser display the alt text in the page? If so,
just put the text there:

document.write(
'<div style="text-align: center;"><span>Pic'
+ ' of the Day'<\/span><br><img src="/image/'
+ image + '" alt="Pic of the Day"><\/div>'
);

[...]

And anyone with JavaScript disabled will see nothing. Why not have a
fixed image name in the HTML, then on your server replace the image
daily with a copy of that day's image. There are tricks to force the
browser to download the image rather than use a cached version if that's
an issue.

No JavaScript and much more reliable.

If you must use client-side script, then put a default image where the
pic of the day is going and then use script to modify the src attribute.
That way, users without JS will see the default, e.g.

<div style="text-align: center;"><span>Pic of the Day</span>
<br><img name="dailyPic" src="/image/deafult.jpg"
alt="Pic of the Day"></div>

<script type="text/javascript">
var tImg;
if (document.images && (tImg = document.images['dailyPic'])){
var time = new Date();
var iSrc = (time.getMonth()+1) + '-'
+ time.getDate() + '.jpg';
tImg.src = iSrc;
}
</script>



--
Rob
Group FAQ: <URL:http://www.jibbering.com/FAQ>


Reply With Quote
Reply




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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.