================================================
Applying JavaScript

The good news for many of you I am sure, is that
this will be the last article in this series on
JavaScript. I had avoided the topic in the past
because I know that it can be quite technical.
JavaScript however is unavoidable for anyone that
will spend even a small amount of time creating his
or her own web site. Even if you don't write it
yourself, you will eventually want to add a banner,
counter or some other piece of
JavaScript code.
Hopefully this series will provide you with some
understanding of what that code is doing. And you
never know, maybe one of you will eventually create
a
JavaScript simulated world like the movie "The
Matrix".

To get started, you may want to review the articles
on
JavaScript so far:
www.imswebtips.com/issue65top1.htm
www.imswebtips.com/issue66top1.htm
www.imswebtips.com/issue67top1.htm
www.imswebtips.com/issue70top1.htm

In the past four articles, I have introduced some
of the common elements of most programming
languages and illustrated how they work in
JavaScript. To be concise, these are:

1. Assigning values to a variable.
Var mynumber = (6 * 32) / 5;

2. Testing variables
if(mynumber > 5)
  mynumber = 10;
else
  mynumber = 5;

3. Loops
for(mynumber = 0; mynumber < 10; mynumber++)
  print.document("Did you know my number=" +
mynumber);

4. Functions and subroutines
function myroutine(mynumber) {
 if(mynumber > 10) {
   while(mynumber > 10) {
      print.document("Too big: " + mynumber);
      mynumber--;
   }
 }
 else {
    while(mynumber < 10) {
      print.document("Too small: " + mynumber);
      mynumber++;
    }
 }}


is71fig1

There is a lot more to JavaScript than this but
these are the fundamental components that you will
use in order to accomplish a task.

What are the tasks that can be done? Apart from
simulated worlds, the possibilities are endless.

Here is a JavaScript you can add to your page to
calculate how many days old your visitors are.
(Note. This script was adapted from several scripts
I found at http://javascript.internet.com/)

<script language="JavaScript">
function daycount(yr,m,d) {
 var months=new
Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");

 var today=new Date();
 var todayy=today.getYear();

 // adjustment for Netscape
 if (navigator.appName == "Netscape") today =1900 + todayy;

 var todaym=today.getMonth();
 var todayd=today.getDate();
 var todaystring=months[todaym]+" "+todayd+", "+todayy;
 var paststring=months[m-1]+" "+d+", "+yr;
 var daysold=(Math.round((Date.parse(todaystring)-
Date.parse(paststring))/(24*60*60*1000))*1);
 return daysold;}

var mm = prompt('What month were you born in?','1-12');
var dd = prompt('What day were you born on?','1-31');
var yy = prompt('What year were you born in?','1975');
var daysold = daycount(yy,mm,dd);

document.write("You are: " + daysold + " days old.<br>");

</script>

is71fig2 http://www.imswebtips.com/iss71fig2.htm

If you look at the code, you will notice that there
are assignments, tests and a function call. The key
to this particular script is the function call to
assign today's date to the variable "today". This
is one of many system functions that you can use.
If you print the variable as is
"document.write(today);" you will get a complete
date string:
"Wed Dec 6 13:50:01 EST 2000"

How valuable is this particular JavaScript?
Actually I am being self- serving because I can
adapt this function to make it into something very
useful on our web site.

We try to post these articles every week on the IMS
Web Tips site. If you have ever bothered to look at
the URL's for the articles, you will notice that
they all end in a number that includes the issue.
This article will be posted by the end of the week
as "issue71top1.htm". If I could write a JavaScript
program to automatically generate a link to these
pages, I could add it to our web sites so that it
would automatically update the link each week. The
'daycount()' function is ideal for this purpose.

Since we are at issue 71, I need to know the date
70 weeks ago. That would be Monday August 2 1999 (I
must have missed a couple). I can now find the week
by dividing the return day count by 7.

var week = parseInt(daycount(1999,08,02) / 7);

Since division will generate a fraction, the
parseInt function is used to generate only the
integer portion, which will increase by 1 every
Monday. Now I can use the value to generate a HTML
link.

document.write('This weeks design article" target="resource window">This weeks design article" target="resource window">This weeks design article" target="resource window">This weeks design article" target="resource window">This weeks design article</a></a></a></a>" target="resource window">This weeks design article" target="resource window">This weeks design article" target="resource window">This weeks design article" target="resource window">This weeks design article" target="resource window">This weeks design article</a></a>" target="resource window">This weeks design article" target="resource window">This weeks design article</a></a></a></a></a>" target="resource window">This weeks design article" target="resource window">This weeks design article" target="resource window">This weeks design article</a></a>" target="resource window">This weeks design article" target="resource window">This weeks design article" target="resource window">document.write('This weeks promotion article" target="resource window">This weeks promotion article" target="resource window">This weeks promotion article" target="resource window">This weeks promotion article" target="resource window">This weeks promotion article</a></a></a></a>" target="resource window">This weeks promotion article" target="resource window">This weeks promotion article" target="resource window">This weeks promotion article" target="resource window">This weeks promotion article" target="resource window">This weeks promotion article</a></a>" target="resource window">This weeks promotion article" target="resource window">This weeks promotion article</a></a></a></a></a>" target="resource window">This weeks promotion article" target="resource window">This weeks promotion article" target="resource window">This weeks promotion article</a></a>" target="resource window">This weeks promotion article" target="resource window">This weeks promotion article" target="resource window">document.write('This weeks IMS article" target="resource window">This weeks IMS article" target="resource window">This weeks IMS article" target="resource window">This weeks IMS article" target="resource window">This weeks IMS article</a></a></a></a>" target="resource window">This weeks IMS article" target="resource window">This weeks IMS article" target="resource window">This weeks IMS article" target="resource window">This weeks IMS article" target="resource window">This weeks IMS article</a></a>" target="resource window">This weeks IMS article" target="resource window">This weeks IMS article</a></a></a></a></a>" target="resource window">This weeks IMS article" target="resource window">This weeks IMS article" target="resource window">This weeks IMS article</a></a>" target="resource window">This weeks IMS article" target="resource window">This weeks IMS article" target="resource window">document.write('Last weeks design article" target="resource window">Last weeks design article" target="resource window">Last weeks design article" target="resource window">Last weeks design article" target="resource window">Last weeks design article</a></a></a></a>" target="resource window">Last weeks design article" target="resource window">Last weeks design article" target="resource window">Last weeks design article</a></a></a>" target="resource window">Last weeks design article" target="resource window">Last weeks design article" target="resource window">Last weeks design article" target="resource window">document.write('Last weeks promotion article" target="resource window">Last weeks promotion article" target="resource window">Last weeks promotion article" target="resource window">Last weeks promotion article" target="resource window">Last weeks promotion article</a></a></a></a>" target="resource window">Last weeks promotion article" target="resource window">Last weeks promotion article</a></a></a></a><br>');
document.write('Last weeks IMS article" target="resource window">Last weeks IMS article" target="resource window">Last weeks IMS article</a></a>" target="resource window">Last weeks IMS article</a></a><br>');

is71fig3 http://www.imswebtips.com/iss71fig3.htm

Will this work? It should. I will post it on the
main IMS Web Tips site at http://www.IMSWebTips.com
so that you can try it and let me know if the year
3000 bug gets it messed up. With a little effort at
some point in the future I will extend the script
to read the title from each page and use it for the
link. Every week the link text would then change to
display the actual title of the topic.

You of course are welcome to use either of these
scripts on your site. Now that you know a little
JavaScript you should be able to modify them for
your own purposes.

Here are a few web sites with more
JavaScript
resources and help.

developer.netscape.com
javascript.internet.com
rummelplatz.uni-mannheim.de


================================================
"IMS Web Tips" ISSN 1488-7088
&copy; Copyright 2000 Virtual Mechanics
================================================

"IMS Web Tips" is a weekly news letter for all web site managers regardless of experience who are looking for detailed information on creating, maintaining and promoting their web sites.

To subscribe send an email to
join.imswebtips@list.imswebtips.com or visit www.IMSWebTips.com for subscription information and a list of past articles.

---------------------------------------------------------------------

If you like the contents of this newsletter, please recommend it to a friend. Not only will you help us to continue to provide you with useful and informative articles, you could also win $10,000. Click here for details.
Click here for a free WYSIWYG pixel precision HTML editor.
Home Page | Archive by Date | Archive by Topic | Virtual Mechanics | DHTML Magic | SVG Magic