Publishers of technology books, eBooks, and videos for creative people

Home > Articles

Language Essentials

  • Print
  • + Share This
This chapter is from the book

Now that you've gotten your feet wet, let's wade a bit deeper into the JavaScript language. In this chapter, we'll go into more detail about the basic elements of JavaScript, and introduce you to other aspects of the JavaScript language, such as loops and functions (don't let your eyes glaze over; we promise that it'll be easy).

You'll see how you can check for the presence of browser plug-ins, use JavaScript to write your Web pages for you, learn how JavaScript handles errors that the user makes, put multiple scripts on a page, and much more.

Table 3.1. Just Enough HTML—The Basics

TAG

ATTRIBUTE

MEANING

table

 

Presents tabular data on a Web page

 

cellpadding

The amount of space inside a cell, between the contents and the cell borders

 

cellspacing

The amount of space between different cells

tr

 

Begins a row inside the table

 

valign

The vertical alignment of the row

th

 

Heading cells for the columns in the table

 

bgcolor

Background color of this header cell

td

 

Contains each cell in the table

 

bgcolor

Background color of this cell


Detecting Browser Plug-ins

(Netscape and Internet Explorer/Mac)

Browser plug-ins provide the Web page author with lots of great options for including rich media content, such as sounds, video, and animation. The problem plug-ins pose is that you can't be sure if the user has the plug-ins installed that you require. JavaScript can help you find out which plug-ins the user has installed, and if they don't have what's needed, you can send them to a page where they can get the required plug-in. Script 3.1 tests for the presence of the Macromedia Flash plug-in, one of the most common plug-ins.

03fig01.jpg

Script 3.1 Testing for plug-ins will soon become second nature with a script like this.

To check for a plug-in

  1. if (navigator.plugins["Shockwave
    U2192.GIF Flash"]) {
    
    Because the script is posing a test, it uses a conditional if statement. In this case, it checks to see if the navigator.plugins array contains the name "Shockwave Flash"; if so, the test returns a true result; i.e., the Flash plug-in is present. Note that if the match isn't exact, then the test will return false. If Macromedia changes the name of the plug-in, you'll need to change your script.
  2. document.write("<object classid=
    U2192.GIF 'clsid:D27CDB6E-AE6D-11cf-96B8-
    U2192.GIF 444553540000' codebase='http://
    U2192.GIF active.macromedia.com/flash2/cabs/
    U2192.GIF swflash.cab#version=2,0,0,0'>
    U2192.GIF <param name='movie' value='images/
    U2192.GIF airport.swf'><embed pluginspage=
    U2192.GIF 'http://www.macromedia.com/
    U2192.GIF shockwave/download/' src='images/
    U2192.GIF airport.swf'><\/embed><\/object>")
    
    Because Flash is present, the script writes a bit of HTML to the document window. Flash requires the parameters of the <object> tag, such as classid, codebase, and param. The name of the file is in the value parameter; it is images/airport.swf. The <embed> and <object> tags are used to put Flash movies into Web pages.
  3. else { document.write("<img src='images/
    U2192.GIF airport.gif' alt='airport'
    U2192.GIF width='414' height='300' \/>")
    }
    
    Here's where the script deals with the possibility that Flash isn't available. If that's the case, a GIF image is substituted for the Flash movie ( Figure 3.1 ).
    03fig02.gif

    Figure 3.1 JavaScript discovered that Flash wasn't present, and displayed a GIF image instead.

  • + Share This
  • 🔖 Save To Your Account