A repeat loop cycles though a group of statements until some condition
is met. For instance, you can use a repeat loop to cycle though an array
and print it to the screen. Or, you might want to cycle though the Array
and look for some specific information using the if statement. The for
loop is the most common used repeat loop. Here is the syntax for it.
The square bracket means that the parameter is optional. I always use all three parameters and think you will too.
The initial expression defines the starting point of the of the loop. It sets the value of a variable to some value. The condition tests the variable for a certain condition and when it is reached stops the loop and lets the code after the for loop execute. The update expression determines how the variable is incremented.
Here is a simple example. We will use the "for loop" to print the numbers 1 through 5 to our screen.
Here is code
and here are the results.
You can declare the variable as part of initial expression.
In fact this is usually the way it is done. Here is our simple script with
Try these two problems in our lab.
Once you have done all of these you can take a look at my solutions to see if I did it right.
One of the neat things you can do using a "for loop" is to generate a calendar like the one shown below.
This is the most complex script that we have done. If you exam it carefully and look at it in sections by the comments I have, I think you will realize that it really is not all that complicated. I first create three Array objects to contain data we will need later. I then create a date object for the first day of the month using the parameters, month and year, that were passed to our function. Then I get the number that represents the day of the week that the first of month falls on. Here I get a little tricky. I make this a negative number. I do this so that later when we put it in a loop that will be incremented by one, I can check this number with an if statement and print a space if it is below 0 or the number if it is greater than 0. That's how I get the blank cells in the table before the 1st of the month.
The rest of the script is used to construct a string that will be returned to be printed to the document. I use a series of loops to construct a couple of tables. The first one is formatting to line up the Month and Year display at the top of calendar with the calendar itself. The second table is the calendar. Notice I did use a break statement close to the end of the script. This was because I did not know how many rows would be needed and I used it to stop making the rows.
Here is the script I put in the body of the document. I used a date
object to first determine what this month and year is and passed these
values to the function. You can call the function without doing this if
you have a particular month and year you want to print a calendar for.
Note that the if statement
Here is how you would call the function to print a specific month, for
example February of 2000 (note that a 4 digit year is used).
Here is the result.
loop tests the supplied condition and continues to execute
until it is met. Here is the syntax for it.
Here is the syntax for the do-while loop.
The primary difference between these two loops is that the "do-while loop" will always execute one time whether the condition is true or not whereas the "while loop" will only execute if the condition is true.
I could have used one of these loops to replace the "for loops" in our calendar. This would have eliminated the need for the break statement. However, I am not convinced that either of these loops have a lot to offer over the "for loop". Again, it is your choice.
Be aware that when you are using any of the loops that you can set up a condition that will cause a continuous loop if you are not careful with your coding. This will cause your page to hang up. So make sure that the condition that will stop execution of the loop is a realistic condition that will be met.
The language property of the SCRIPT tag can be used to isolate scripts
For example, suppose you write a great function that only works in
tags for version 1.2. You must also include the top tag to accommodate
older browsers with a different version of your function in it. Here is
what it would look like.
There will be times when you need to be able to detect information about the browser a surfer is using. Most of the time this will be so that you can initiate different portions of your scripts because of the inconsistency of versions between the browsers. You will use the navigator object to accomplish this.
The navigator object has 8 properties which are listed below with the value for the particular Browser you are now using.
The appName property contains the official name of the browser application. This is Netscape for Netscape browsers and Microsoft Internet Explorer for Internet Explorer Browsers. Both browsers display Monzilla for the appCodeName property. The appVersion and userAgent contain information on the browser's version, platform of the Browser, and the country for which the browser is released. The language property contains a two letter code to indicate the localized language version of the program. The platform property reflects the operating system but is only available beginning with the version 4 browsers. The mimeTypesand plugings are both array objects. The mimeTypes contains a listing of files of the type MIME (Multipurpose Internet Mail Extension) that is quite extensive. I just showed the length property for it here. I really don't know of what use this information could be. The plugins object contains information on the plugins that are available in Netscape. I listed its name property above. It also has three other properties and one method.
If you view this using Internet Explorer, you will notice that the language property above is undefined. Also, the number of mimeTypes will be shown as 0 and the plugins will be blank. This means that these three properties will not be very useful to you. However, the appName, appVersion and userAgent properties are available in all browsers, since Netscape 2 and Internet Explorer 3. These three properties should be useful for determining information on browsers and platforms.
The navigator object has three methods, JavaEnabled(), preference(), and taintEnabled(), which will not be covered here because I feel there intended use is more advanced.
[ Top of Page | Contents ]