Welcome to WebmasterWorld Guest from 107.20.34.173

Forum Moderators: open

Message Too Old, No Replies

JavaScript - certain days - different images

JavaScript - certain days of the year that shows different images

     

andy04031

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

10+ Year Member



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

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

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



<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

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

WebmasterWorld Senior Member 10+ Year Member



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

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

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

madcat

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

WebmasterWorld Senior Member 10+ Year Member



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

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

10+ Year Member



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

DrDoc

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

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

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

WebmasterWorld Senior Member 10+ Year Member



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

[webmasterworld.com ]

OhMyPixel

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

10+ Year Member



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

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

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

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

WebmasterWorld Senior Member 10+ Year Member



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

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

10+ Year Member



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

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

WebmasterWorld Senior Member 10+ Year Member



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

DrDoc

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

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

Featured Threads

Hot Threads This Week

Hot Threads This Month