|
To access the contents, click the chapter and section titles.
HTML 4.0 Sourcebook
With the Navigator 2 browser, Netscape introduced the concept of scripted HTML documents. These are documents that contain, in addition to regular HTML markup, script programs that can process data passed from the HTML document (i.e., from FORM input, hypertext anchor selections, etc.) and that can also produce output for inclusion within the HTML document, as it is rendered for display. To implement this functionality, Netscape developed a new scripting language, called JavaScript. This is an objectderived language, similar in look and feel to Java or C++ but designed expressly for Web and HTML documentrelated scriptingthe object model of JavaScript is based on the component structure of the browser (windows, status bars, document URLs, etc.) and of HTML documents (applets, embedded objects, forms, anchors, document body, images, etc.). Microsoft quickly followed up Netscapes launch of scripted HTML, by implementing JavaScript support in their Internet Explorer 3 browser. However, Microsoft did not have access to all the details of the JavaScript language, so that their browser was not entirely compatible with the version of JavaScript supported by Netscapeindeed, Microsoft formally named their language Jscript to denote this difference. Thus, if you are working in a mixedplatform environment, it is important to check your JavaScript programs with all possible browsers. Microsoft also implemented a second scripting language, known as VBScript (or Visual Basic Script). In terms of functionality, VBScript is similar to JavaScript, although the language is closer in design to Visual Basic, from which it is derived. However, the interaction between a document and a scripting language is largely independent of the language, so that the information presented here is also largely applicable to VBScript programs. With the release of Netscape Navigator 4 and Internet Explorer 4, script compatibility became both better and worse. It became better in that both browsers support a similar core functionality, which is more or less the functionality of Netscape Navigator 3, plus some extensions. This compatible scripting component is the main topic of this section. However, both companies then added significant scripting capabilities that allow for much richer interaction between script programs and the HTML document. Unfortunately, the enhancements are entirely incompatiblethe new Microsoft extensions do not work with Netscapes browser and vice versa. These scripting extensions are commonly called DynamicHTML and we briefly review the issues associated with this at the end of the section. Please note that this is not a guide to JavaScript programmingthe language is far too complex for this short section to do it justice. For details about the language, you are referred to the references listed at the end of the chapter. General Scripting IssuesAs mentioned in Chapter 4, document scripts are included within an HTML document via the HTML SCRIPT element. These elements can be in the HEAD or BODY of a document, but it is strongly recommended that the bulk of the code appear in the HEAD. This is because of they way scripts are interpreted and executed. When a browser parses a document for script components, it starts at the beginning of the file and works downwards. As a result, any required functions must be defined before they are usedif this is not done and the function is not defined at the time of its intended invocation, then the script will fail, claiming that the function is undefined. Consequently, function definitions are best placed in the HEAD of a documentthat way they are guaranteed to be defined before the browser begins to layout the document BODY. Script programs can be included directly within an HTML document, or they can be included from external files. Some examples illustrating these two mechanisms are given in the following sections. Including a Script Within a SCRIPT ElementFigure 7.38 illustrates a simple JavaScript function definition, defined in a SCRIPT element that typically would lie in the document HEAD (since it defines a generalpurpose function). This simple example illustrates a few of the important points which are relevant whenever a script is included within an HTML document. There are three critical things to note in Figure 7.38:
With respect to the actual JavaScript language, this example illustrates how variables (var) and functional methods (function() ) are defined and shows how alerts (popup alert notifications) are invoked (alert() ). JavaScript variables are untypedas with several other scripting languages (e.g., Perl), variable type is determined by context. Figure 7.38 Example SCRIPT element containing a single function definition. Note the use of an HTML comment (the tag is in boldface) to hide the element content from old browsers that do not understand the SCRIPT element. <SCRIPT LANGUAGE=JavaScript> <! Use Comments to hide script from old browsers // that dont understand scripts // The symbol // starts a comment line function validate(obj, minV, maxV)) { var value = parseInt(obj.value); if( value < minVal || value > maxVal) { alert(obj.name + Not in range (+minV+,+maxV+) ); } } // End HTML comment that hides the script from old browsers > </SCRIPT> Using SCRIPT Within the BODY Script components in the document BODY can actually print out text for inclusion with the displayed HTML. A simple HTML document example that uses the JavaScript Date() function to print the current time and date is shown in Figure 7.39. Figure 7.40 shows the resulting document displayed by Netscape Navigator, while Figure 7.41 shows the document displayed by a browser that does not understand SCRIPT elements.
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |