Basics

Scripts

JS Links












































The HTML Writers Guild


Email Sunshine


New Item Icon

When you add something to your site or modify something, you usually place a new icon next to it so that visitors can easily find it. The idea is to display the icon for a fixed period of time and then remove it. However, if you are constantly modifying your site or if you have a large number of pages, then keeping track of all the new icons becomes very difficult.

Using JavaScript, it is possible to program your HTML to display the new icon only for a certain period of time. You write a JavaScript function which compares the current date with the expiry date and inserts the new icon only if the current date has not exceded the expiry date. Here is the code :

<SCRIPT LANGUAGE="JavaScript">
    <!-- Hide code from non-js browsers
    function newItem(expiryDate)
    {
	exp = new Date(expiryDate);
	cur = new Date();
	if (cur.getTime() < exp.getTime())
	    document.write("<IMG SRC=\"new.gif\" WIDTH=31 
		HEIGHT=12 BORDER=0>" ALT=\"new\");
    }
    // end hiding -->
</SCRIPT>
		
Usually, functions like this are defined between the <HEAD> and </HEAD> part of the HTML. Change the new.gif to the name of your image and modify the WIDTH and HEIGHT attributes accordingly. Now where ever you wan to put the icon, insert the following code :
<SCRIPT LANGUAGE="JavaScript">
    <!--
    newItem("10/1/96");
    -->
</SCRIPT>
The icon will be shown up to the date you specify as the parameter for newItem().

Back to Contents