- Detecting Browser Plug-ins
- Around and Around with Loops
- Writing with Loops
- Using For/In Loops
- Using Do/While Loops
- Checking if Java Is Enabled
- Functions
- Using Multi-level Conditionals
- Working with Functions That Return Values
- Handling Errors
- Putting More than One Script on a Page
- Scrolling Status Bars
- Status Bar Messages
Writing with Loops
(Netscape and Internet Explorer/Mac)
Sometimes you'll want to get information out of JavaScript objects and display them to the user. Using JavaScript to write the user's Web page on the fly is a useful technique, one you'll find yourself using often. In this example ( Script 3.3 ), you'll use a loop to display all of the plug-ins installed in your browser, and write a list of them out to the browser window.

Script 3.3 You can use loops to step through JavaScript objects and write to the browser window.
To write a Web page using a loop
-
if (navigator.plugins && navigator.
If both of these values are true, continue on with the following code.plugins.length > 0) {
-
document.write("You have the
The information on the page will be displayed inside a table, so we'll start it off by writing out the <table> tag here.following plugins: <table
cellspacing='4' cellpadding='4'>")
-
document.write("<tr><th bgcolor=
This line of code writes out the headers for the first row of the table.'#CCCCCC'>Name<\/th><th bgcolor=
'#CCCCCC'>Filename<\/th><th bgcolor=
'#CCCCCC'>Description<\/th><\/tr>")
-
for (i=0; i<navigator.plugins.length;
This line, as in the previous example, loops through each installed plug-in.i++) {
- thisPlugin = navigator.plugins[i] Rather than refer to each plug-in by its full name, we set up a temporary variable that saves the value of the plug-in that we're currently interested in.
-
document.write("<tr valign='top'>
This line starts a new row and cell, and writes out the name of the current plug-in.<td bgcolor='#CCCCCC'>" +
thisPlugin.name)
-
document.write("<\/td><td bgcolor=
The old table cell is ended, a new one begins, and the filename of the plug-in is written to the page.'#CCCCCC'>" + thisPlugin.filename)
-
document.write("<\/td><td bgcolor=
Another cell ends, a new one begins, and the description of the plug-in finishes off the cell and row.'#CCCCCC'>" + thisPlugin.
description + "<\/td><\/tr>")
- } This brace ends the loop, and the lines inside are repeated for each plug-in installed in the user's browser.
-
document.write("<\/table>")
When the loop has finished, we write out the ending <table> tag to finish off the code, as shown in
Figure 3.5
.
Figure 3.5 This table of plug-ins was written to the browser window using a loop.
-
document.write("JavaScript couldn't
If the user has a browser that doesn't support viewing the plug-ins through JavaScript, this is the message they'll see.find any plugins")