Chapter 3:
Tag...You’re It!

In This Chapter

Giving HTML a Caffeine Boost

If you're interested in learning JavaScript programming (and I assume you are; this is the wrong book to be reading just for fun), you must be at least somewhat familiar with HTML by now. To quickly summarize, HTML is a markup language used to "describe" how a Web page should look. HTML is expressed through tags, which are written in the form:

Most tags surround the text on which they operate and, therefore, have to be paired in the manner:

Web browsers such as Netscape and Internet Explorer interpret these markup tags and render the page to the screen according to the instructions built into the tags. 

If this is material is new or surprising, I strongly recommend that you read a primer on HTML, such as Que's The Complete Idiot's Guide to Creating an HTML Web Page, by Paul McFedries.

Tag Attributes

Because this chapter refers to attributes of an HTML tag several times, you need to know what they are. 
As stated, a typical HTML tag looks like this: <TAG>. However, many tags accept further specifications 
that determine their final effect, such as:

That's all there is to it. When you use a tag that requires attributes or that can include attributes, it will be
pointed out within the chapter.

The <SCRIPT> Tag

The tag to know for JavaScript programming is the <SCRIPT> tag. In short, it looks like this:

The opening <SCRIPT> tag can include either of two attributes: the LANGUAGE attribute or the SRC attribute. 

This is the standard opening <SCRIPT> tag, with the LANGUAGE attribute:

This tag simply defines the start of JavaScript program code and identifies the language the code is written in (which, obviously, is JavaScript). JavaScript’s official documentation claims that the language attribute is mandatory; you cannot simply begin a section of code with <SCRIPT> alone because it helps the browser identify not only script code, but also the language the code is written in. (Currently, one other scripting language—VBScript from Microsoft—also uses the <SCRIPT> tag.) It should be noted, though, that in practice, omitting the LANGUAGE attribute from the <SCRIPT> tag doesn’t cause any problems in most cases—especially when pages are viewed with a browser that supports only one scripting language (such as Navigator). For the future, however, it would be a good idea to stick to the rule and mark the end of a JavaScript code section with </SCRIPT>.

As an alternative to writing the JavaScript code within the Web page source itself, you can refer to JavaScript code that is saved in its own text file elsewhere on the server. This will behave just as if the code were typed directly into the Web page source (as it is in all of these examples). The following is the general format for such a code, which uses the SRC attribute:

 
The URL (Earl) of Web?
 
URL stands for Uniform Resource Locator. It’s a fancy way of identifying anything on the Internet anywhere in the world (a file, a document, a graphic, or a Web site, for example) with a unique address. Think of it as a global CyberZipCode: No two Web pages, FTP files, UseNet newsgroups, Gopher menus, or whatever can have the same URL.
This would be an efficient way of reusing the same JavaScript code in several Web pages, without having to explicitly enter it into each one. You'd merely insert the call to the file. The text file of the JavaScript program must have a file name ending in .JS, as in the following example:

<SCRIPT SRC="http://www.epicmedia.com/funscript.js"> ... </SCRIPT>

Beware of Outside Sources!

Currently, the SRC= attribute of the <SCRIPT> tag is only supported by Netscape Navigator 3.0 
and greater. If you want your JavaScript pages to work with other browsers, such as Internet 
Explorer 3.0 and earlier versions of Netscape, you might want to avoid using the SRC= attribute 
and incorporating our JavaScript code within your pages. The possible exception to this might be
Internet Explorer 4.0 (which might be released by the time you read this book). It might support 
the SRC attribute; however, at press time, this is not known for certain. 


You place JavaScript code between <SCRIPT> tags wherever you want the code executed within a page. A tiny example of the HTML code of a Web page, with embedded JavaScript code, might look like this:

<HTML>
<HEAD>
<TITLE>My Nifty Web Page</TITLE>
</HEAD>
<BODY>
<H1>
Welcome to my Exciting Home Page,<BR>
Where I talk about ME
</H1>
<HR>
<SCRIPT LANGUAGE="JavaScript">
JavaScript program code goes here
</SCRIPT>
</BODY>
</HTML>
Note that you needn't put all of the JavaScript code within one set of <SCRIPT> tags. You can use numerous <SCRIPT> </SCRIPT> pairs, inserting them wherever in the page you want to include JavaScript code. Having said that, there are three caveats to note:
<HTML>
<HEAD>
<TITLE>My Funky JavaScript Page</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function func1(x, y, z) {
statements
}
function func2(a, b, c) {
statements
}
...
</SCRIPT>
</HEAD>
...
  • Functions must be defined before they can be called. (You might not understand what that means just yet, but after you read Chapter 8, the skies will be clear.) To summarize, a function is a section of code that performs a specific task. It is like a self-contained miniprogram. For many reasons, in other areas of JavaScript code, you will find the need to call these functions—that is, refer to them by name and have them executed. You'll learn about functions in gruesome detail in Chapter 8. In the meantime, simply remember that following this rule will ensure that all functions have been defined before they are called in any JavaScript code further down the page.
  • Can You Keep a Secret?

    With all the concern about security in CyberSpace these days, you're probably wondering about the
    "secrecy" of JavaScript. After all, if your prized script is written in the middle of an HTML file, all someone 
    has to do is select the View Source option from the browser’s menu, and your hard work is displayed plain
    as day...right? 

    Unfortunately, that is right. If you want to keep your script from being seen, place it in an external file 
    and include it using the SRC= attribute of the <SCRIPT> tag. But, remember two important things when
    you do this:

      • Make sure that the file ends in .JS.
      • Understand that only users of Netscape Navigator 3.0 will be able to use your pages 
        correctly (as neither Navigator 2.0 nor Internet Explorer 3.0 support the SRC= attribute).

    • Any and all JavaScript code, including function definitions, applies only to the page it resides on. If you wanted to use the same JavaScript code in more than one page (and didn't want to type it over and over again), you could either save the JavaScript code as an external file and use the SRC= attribute of the <SCRIPT> tag to load it, or store the JavaScript code in one document of a framed collection of documents and reference the JavaScript functions from the other framed documents.
    • Enclose all JavaScript code within HTML comment tags. An HTML comment tag looks like:
      • <!-- comments -->
    • Anything within HTML comment tags is ignored by most browsers—except browsers that support JavaScript, which will recognize the presence of the JavaScript code and not ignore it. The rationale behind this is that those browsers that know nothing of JavaScript might display the code in the Web page itself, which would not be desirable at all. This also can be used to allow all browsers—whether or not they’re JavaScript-capable—to view the contents of your page (if the contents itself can be viewed without the JavaScript support). Therefore, in the end, the recommended manner of including JavaScript code into a page is as follows:
  • <SCRIPT LANGUAGE="JavaScript">
    <!--
    JavaScript program code goes here
    // -->
    </SCRIPT>

    It doesn’t matter where your JavaScript statements go in your page; they are always evaluated (a fancy term for "made ready to run") after the page has finished loading into the browser (but before the browser actually displays the page to the user).

    Rules of the Web

    You need to keep some fundamental rules in mind when you're building your JavaScript pages:

    The Least You Need to Know

    This chapter has introduced you to the new <SCRIPT> tag that extends HTML to allow you to plug JavaScript commands into your Web pages. You also learned the following rules about scripts:

    Previous Chapter Next Chapter

    Beginning of ChapterTable of Contents Que Home Page


    For comments or technical support for our books and software, select Talk to Us.
    To order books, call us at 800-716-0044 or 317-228-4366.

    1997, QUE Corporation, an imprint of Macmillan Publishing USA, a Simon & Schuster Company.