First - J Script
First - J Script
JavaScript Script
3
C H A P T E R
✦ ✦ ✦ ✦
I
In This Chapter
n this chapter, you set up a productive script- writing and
previewing environment on your computer — and then Choosing basic
write a simple script whose results you will see in your JavaScript authoring
JavaScript-compatible browser. tools
Because of differences in the way various personal
computing operating systems behave, I present details of Setting up your
environments for two popular variants: Windows 95 and the authoring
MacOS. For the most part, your JavaScript authoring environment
experience will be the same regardless of the operating
system platform you use — including UNIX. Although there Entering a simple
may be slight differences in font designs depending on your script to a Web page
browser and operating system, the information will be the
same. All illustrations of browser output in this book are ✦ ✦ ✦ ✦
made from the Windows 95 version of Netscape Navigator 4.
If you’re running another version of Navigator, don’t fret if
every pixel doesn’t match with the illustrations in this book.
Choosing a browser
The other component required for learning JavaScript is the browser. You don’t
have to be connected to the Internet to test your scripts in the browser. You can
do all of it offline. This means you can learn JavaScript and create cool-scripted
Web pages with a laptop computer, even on a boat in the middle of an ocean.
The only requirement for the browser is that it be compatible with the current
release of JavaScript. The instructions and samples in this book are designed for
Netscape’s Navigator 4. Microsoft’s Internet Explorer 4 supports most of the core
language of Navigator 4, but you’ll still find occasional incompatibilities.
Windows
You don’t have to have either the editor or browser window maximized (at full
screen) to take advantage of them. In fact, you may find them easier to work with if
you adjust the size and location of each window so both windows are as large as
they can be while still enabling you to click on a sliver of the other’s window. Or, in
Windows 95, leave the taskbar visible so you can click the desired program’s
button to switch to its window ( Figure 3-1). A monitor that displays more than 640
× 480 pixels certainly helps in offering more screen real estate for the windows and
the taskbar.
MacOS
If you expand the windows of your text editor and browser to full screen, you
have to use the rather inconvenient Application menu (right-hand icon of the menu
bar) to switch between the programs. A better method is to adjust the size and
location of the windows of both programs so they overlap, while allowing a portion
of the inactive window to remain visible ( Figure 3-2). That way, all you have to do
is click anywhere on the inactive window to bring its program to the front.
Figure 3-2: Editor and browser window arrangement on the Macintosh screen
Below the rule, the script displays plain body text that combines static text with
information about the browser you used to load the document. The script writes a
stream of HTML information to the browser, including a tag to render a portion of
the information in boldface. Even though two lines of code are writing information
to the page, the result is rendered as one line, just as it would be if all the text had
been hard-coded in HTML.
6 Part I ✦ Getting Started with JavaScript
<BODY>
<H1>Let's Script...</H1>
<HR>
<SCRIPT LANGUAGE="JavaScript">
<!-- hide from old browsers
document.write("This browser is version " + navigator.appVersion)
document.write(" of <B>" + navigator.appName + "</B>.")
// end script hiding -->
</SCRIPT>
</BODY>
</HTML>
3. Save the document with the name script1.htm. ( This is the lowest common
denominator file-naming convention for Windows 3.1 — feel free to use an
.html extension if your operating system allows it.)
4. Switch to your browser.
5. Choose Open File from the File menu and select script1.htm.
If you typed all lines as directed, the document in the browser window should
look like the one in Figure 3-3 (with minor differences for your computer’s
operating system and browser version). If the browser indicates that a mistake
exists somewhere as the document loads, don’t do anything about it for now
(click the OK button in the script error dialog box). Let’s first examine the details
Chapter 3 ✦ Your First JavaScript Script 7
of the entire document so you understand some of the finer points of what the
script is doing.
of this containment is not clear until you see what happens to your scripted HTML
document in a non–JavaScript-compatible browser. Such a browser would blow
past the <SCRIPT> tag as being an advanced tag it doesn’t understand. But it
would treat a line of script as regular text to be displayed in the page. By enclosing
script lines between HTML comment tags, most older browsers won’t display the
script lines. Still, some old browsers can get tripped up and present some ugliness
because they interpret any > symbol (not the whole --> symbol) to be an end-of-
comment character. Figure 3-4 shows the results of your first script when viewed in
a now-obsolete version of the America Online Web browser (version 2.5 for
Windows).
Figure 3-4: By enclosing script lines between HTML comments, the entire script is
ignored by most, but not all, non-JavaScript browsers. Here, an old America Online
browser shows part of the script anyway.
Remember, too, that some users don’t have access to modern browsers or
graphical browsers (they use the Lynx text-oriented UNIX Web reader software or
Lynx-like browsers in hand-held computers). By embracing your script lines within
these comments, your Web pages won’t look completely broken in relatively
modern non-JavaScript browsers.
Notice that the comment lines that shield older browsers from your scripts go
inside the <SCRIPT>...</SCRIPT> tags. Do not put these comment lines above
the <SCRIPT> tag or below the </SCRIPT> tag and expect them to work.
One more issue about the script-hiding comment lines in this book. To save
space on the page, most examples do not have comment lines inserted in them.
But as you can see in the full-fledged application examples on the CD-ROM (in the
folder named Bonus Applications Chapters), the comment lines are where they
should be. For any pages you produce for public consumption, always encase your
script lines inside these comments.
Chapter 3 ✦ Your First JavaScript Script 9
The line of text that the script writes starts with some static text (“This
browser is version“) and adds some evaluated text (the version of the
browser) to it. The writing continues with more static text that includes an HTML
tag (“of <B>”), more evaluated text (the name of the browser application), and
ends with an HTML closing tag and the sentence’s period (“</B>.”). JavaScript
uses the plus symbol (+) to join (concatenate) text components into a larger, single
string of text characters to be written by the document. Neither JavaScript nor the
+ symbol knows anything about words and spaces, so the script is responsible for
making sure that the proper spaces are passed along as part of the parameters.
Notice, therefore, that an extra space exists after the word “version” in the first
document.write() parameter, as well as spaces on both sides of “of” in the
second document.write() parameter.
To fetch the information about the browser version and name for your
parameters, you call upon JavaScript to extract the corresponding properties from
the navigator object. You extract a property by appending the property name to
the object name (navigator in this case) and separating the two names with a
period. If you’re searching for some English to mentally assign to this scheme as
you read it, start from the right side and call the right item a property “of” the left
side: the appVersion property of the navigator object. This dot syntax looks a
great deal like the document.write() task, but a property name does not have
parentheses after it. In any case, the reference to the property in the script tells
JavaScript to insert the value of that property in the spot where the call is made.
For your first attempt at the script, JavaScript substitutes the internal information
about the browser as part of the text string that gets written to the document.
how the script has changed the text in the document. Feel free to substitute other
text for the quoted text in the document.write() statement. Or, add more text
with additional document.write() statements. The parameters to document.
write() are HTML text, so you can even write a “<BR>” to make a line break.
Always be sure to save, switch, and reload to see the results of your handiwork.
✦ ✦ ✦