homepage Welcome to WebmasterWorld Guest from 54.167.41.199
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
JavaScript - certain days - different images
JavaScript - certain days of the year that shows different images
andy04031




msg:1491485
 1:50 am on Jun 19, 2002 (gmt 0)

Hi everybody,
does anyone have any idea if how I can realize that with JavaScript:

I want to show only on certain days of the year different images e.g. 4 of July should be a image with an US Flag there, on the 5. appears the "standard" image until maybe halloween - of course a halloween picture, until christmas standard again but on the 25. of Dec the xmas image .... and so on.

I don't want to make the changes always manual, I want to create and upload all the images and put the JavaScript in and thats it....am I dreaming or possible?
Any help is very appreciated, thanks, Andy

 

DrDoc




msg:1491486
 2:39 am on Jun 19, 2002 (gmt 0)

<script language="javascript" type="text/javascript">
now = new Date();
month = now.getMonth() + 1;
day = now.getDate();
if(month == 7 && day == 4) {
document.write('<img src="images/jul4.jpg" alt="July 4" />');
}
else if(month == 10 && day == 31) {
document.write('<img src="images/oct31.jpg" alt="October 31" />');
}
else if(month == 12 && day == 25) {
document.write('<img src="images/dec25.jpg" alt="December 25" />');
}
/*
Insert more else if lines here
Follow the examples above when you check for month and day
*/
else {
document.write('<img src="images/standard.jpg" alt="" />');
}
</script>

madcat




msg:1491487
 2:54 am on Jun 19, 2002 (gmt 0)

You could put that cool script in an external .js file too (without the <script> tags).

DRDOC
function changePicture() {
now = new Date();
month = now.getMonth() + 1;
day = now.getDate();
if(month == 7 && day == 4) {
document.write('<img src="images/jul4.jpg" alt="July 4" />');
}
else if(month == 10 && day == 31) {
document.write('<img src="images/oct31.jpg" alt="October 31" />');
}
else if(month == 12 && day == 25) {
document.write('<img src="images/dec25.jpg" alt="December 25" />');
}
else {
document.write('<img src="images/standard.jpg" alt="" />');
}
}

I suggest something like this in your <div> or <table>...

<script type="text/javascript">
//<![CDATA[
changePicture();
//]]>
</script>

M

DrDoc




msg:1491488
 3:03 am on Jun 19, 2002 (gmt 0)

.. except that if you put the actual script in an external js file, you don't need the CDATA lines anyway ;)

madcat




msg:1491489
 3:15 am on Jun 19, 2002 (gmt 0)

Actually, lol - I'm failing to understand the CDATA designation again. Is it used only for scripts declared in the <head> rather than external scripts? Would you mind explaining a bit further?

Thanks,
M

andy04031




msg:1491490
 4:12 am on Jun 19, 2002 (gmt 0)

guys, thank you very much for your help, I'll try this out and let you know, thanks again, Andy

DrDoc




msg:1491491
 6:33 am on Jun 19, 2002 (gmt 0)

madcat,

You should use <![CDATA[ ]]> if you are using any special characters, such as [, ], >, <, and &, in your script.

However, if you put the script in an external js file you don't have to.

[w3.org...]
[xml.com...]
[webmasterworld.com...]

madcat




msg:1491492
 2:49 pm on Jun 19, 2002 (gmt 0)

Thanks for the links DrDoc, I got it. But this was the problem I was having earlier...

[webmasterworld.com ]

OhMyPixel




msg:1491493
 3:30 pm on Jun 19, 2002 (gmt 0)

If CDATA is going to cause problems - would it be bad to consider putting all JavaScript in an external .js file from now on? Sounds like another browser problem (yay!....shoot me).

-OMP

DrDoc




msg:1491494
 1:32 am on Jun 20, 2002 (gmt 0)

well, the reason why Opera couldn't see the JS is because you had the comments ..

<script type="text/javascript">
<!--
changePicture();
//-->
</script>

Remove the comments, and you'll be fine ;)

Purple Martin




msg:1491495
 2:58 am on Jun 20, 2002 (gmt 0)

That's something new to me, DrDoc. I've always included the <!-- and //--> comments inside script tags (probably because I started out using Dreamweaver which puts them in). Are you saying that Opera will be ignoring any JavaScript inside those comments? I had thought that the comments were necessary to stop non-JavaScript-supporting browsers falling over.

rewboss




msg:1491496
 9:45 am on Jun 20, 2002 (gmt 0)

The problem relates not to browsers that don't support JavaScript, but browsers that don't recognize the <script> tag. There's a subtle difference.

When JavaScript was first introduced, most browsers didn't recognize the <script> tag, so they ignored the tag and happily displayed the JavaScript code, blissfully unaware of the fact that this was not intended. Hence the need for the comments in the days when Netscape 2 ruled supreme.

Later browsers that didn't support JavaScript (I believe that includes WebTV, is that right?) did recognize the <script> tag. But to them, it meant (and still means) "ignore this, it's not for you".

On the basis that most browsers that didn't recognize <script> have probably gone out of circulation, you can probably safely omit the HTML comments.

Incidentally, one early version of the Lynx browser had real problems, because not only did it not recognize the <script> tag, but it also ended the HTML comment at the first > it came to. This meant that this code:

<script>
<!--
if(i>9) document.write('i is greater than 9');
// -->
</script>

generated the following display:

9) document.write('i is greater than 9');

Cross-browser compatibility was a major challenge even back then.

Purple Martin




msg:1491497
 11:59 pm on Jun 20, 2002 (gmt 0)

Thanks for that rewboss! I think I'll drop the comments from now on :)

DrDoc




msg:1491498
 6:28 am on Jun 21, 2002 (gmt 0)

And, if your site is upgraded to XHTML, <!-- --> should not be used.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved