Scripts in HTML

Every element in HTML has a tag, and scripts are no different. In most cases, for the browser to recognize a script and execute it properly, the script must exist inside a set of script tags. Script tags generally look like this:

 <SCRIPT LANGUAGE="JavaScript"> <!-- [The actual script commands] //--> </SCRIPT> 

The first line is the opening script tag. Just as you need an opening tag for a block of boldface text (<B>) or a title (<TITLE>), you need one for a script (<SCRIPT>). The first line also specifies a language, identifying the language being used within that particular script block. (Although JavaScript and JScript can be used interchangeably with Internet Explorer for the LANGUAGE attribute, we specify JavaScript instead of JScript because some other browsers do not recognize the JScript script tag.) As you might guess, you can mix and match script blocks of different languages, such as VBScript and JScript, on the same page. The different blocks can even refer to one another. However, this will incur a slight loading and performance penalty.

The <!-- and --> symbols are the opening and closing comment tags in HTML. These let you set aside text that the browser should not render on the screen. It is a good idea to include comment tags like this around scripts because older browsers that do not recognize scripts will ignore everything inside these comment tags. They are not necessary when producing pages for more recent browsers. Two forward slashes (//) act to comment out a line in JScript, thus, in this case, they comment out the --> tag. This will prevent errors in Netscape Navigator, which tries to execute these characters as script when they reside in a script element. The last line in the sample code is the closing script tag, </SCRIPT>. It serves to define the end of the script, just as the </TITLE> tag serves to end the title of a page.

In addition to enclosing script within opening and closing script tags, you can also point a script tag to an external file containing script. This is accomplished through the SRC attribute, similar to how an <IMG> tag refers to a graphic to display. For example, if you had an external file named jsLibrary.js, you could reference it as follows:

 <SCRIPT LANGUAGE="JavaScript" SRC="jsLibrary.js"></SCRIPT> 

Using a reference to an external script has several advantages. If you have some script that you would like to access from many different pages on your site, you can simply place the script in an external file that is referenced by every such page. By using one source, you avoid the problem of accidentally using different versions of your script in different files. Additionally, once downloaded, the script file is cached (stored locally on the user's machine), which means that the script does not need to be downloaded separately as part of every Web page. This can reduce downloading time substantially. This technique works in Netscape Navigator 3 and later and Internet Explorer 3.02 and later.

Finally, scripts that handle events that occur to specific elements on the page can be incorporated into elements themselves. Such scripts are called embedded event handlers, and will be discussed later in this chapter.



Dynamic HTML in Action
Dynamic HTML in Action
ISBN: 0735605637
EAN: 2147483647
Year: 1999
Pages: 128

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net