|
To access the contents, click the chapter and section titles.
HTML 4.0 Sourcebook
Figure 7.39 A simple HTML document containing a JavaScript block that prints a line of HTML into the displayed document. <HTML><HEAD> <TITLE>Scripting Test</TITLE></HEAD> <BODY> <H2>Scripting Test</H2> <SCRIPT> <! document.write(<H3 ALIGN=right> + Date() + </H3>); // > </SCRIPT> <NOSCRIPT> <P>Sorry, no Scripting Support... </NOSCRIPT> </BODY></HTML>
Looking to Figure 7.39, you should note how the singlequote string delimiters allow HTML elements to be easily included in the write statement. Note also the presence of a new element, NOSCRIPT, just following the SCRIPT. NOSCRIPT can contain regular HTML markup, as an alternative to SCRIPT for those browsers that do not understand the scripting language or the SCRIPT element. Browsers that understand both SCRIPT and NOSCRIPT will hide the NOSCRIPT content. Figure 7.40 shows the utility of this new element. NOSCRIPT is understood by Netscape Navigator 3 and Internet Explorer 3, but not by earlier versions of these browsersthese early versions display the NOSCRIPT content in addition to that produced by SCRIPT. Dynamic Scripting Usually, scripting programs are more dynamic than the example shown in Figures 7.39 and 7.40, with many script programs including a level of user interaction. However, the dynamic character of the document is actually quite limited. Using the standard version of JavaScript (that is, the components understood by both Netscape Navigator and Internet Explorer), textonce drawn to a frame or windowcan only be modified by replacing the entire frame or window with a new document. Fortunately, FORM element text input fields can be dynamically updated by JavaScript programs, without reloading the entire document.
External Script FilesScript programs can be kept in external files and then included into an HTML document. Remote script programs are included using SCRIPT elements of the form: <SCRIPT LANGUAGE=lang_name SRC=URL> </SCRIPT> where lang_name is the name of the scripting language (JavaScript or VBScript), and URL is the URL referencing the script file. Note that the end tag </SCRIPT> is requiredif you leave it out, the script will not load properly. Figure 7.42 shows how the document listed in Figure 7.39 can be rewritten to use an external script and also shows the content of the referenced script file script.js. The language names that are actually understood depend on the browser, and on the version of the browser. For example, Netscape Navigator 3 understands the names Javascript1.1 and Javascript, the first name corresponding to JavaScript as implemented in Navigator 3, and the second to JavaScript as implemented in Navigator 2. Navigator 4 supports the additional value JavaScript 1.2, to correspond to the newest incarnation of JavaScript (note that these strings are caseinsensitive). Internet Explorer, on the other hand, understands the values javascript, jscript, and vbscript, but will interpret any of the javascript1.x values as indicating a JavaScript program. Figure 7.42 HTML document illustrating external JavaScript files. This document is equivalent to the one listed in Figure 7.39, except for the placement of the JavaScript program in the external file script.js. script1.html <HTML><HEAD> <TITLE>Scripting Test</TITLE></HEAD> <BODY> <H2>Scripting Test</H2> <SCRIPT LANGUAGE=javascript SRC=script.js> </SCRIPT> <NOSCRIPT> <P>Sorry, no Scripting Support... </NOSCRIPT> </BODY></HTML> script.js document.write(<H3 ALIGN=right> + Date() + </H3>); Browsers should ignore scripting languages that they do not understand. Thus, Netscape Navigator (all versions) ignores scripts with LANGUAGE=jscript or vbscript, while Netscape Navigator 3 ignores script sections labeled by LANGUAGE=javascript1.2. Note in Figure 7.42 how the external script file does not place the script inside an HTML commentobviously this hiding mechanism is not needed for a script outside an HTML document. However, you can include the HTML comment delimiters within a script file, as they are ignored by a script parser. This is convenient if you are copying scripts between HTML documents and external script files. MIME Types for Script Files An HTTP server distributing script files must be configured to send the correct MIME type associated with the scriptotherwise the browser will not know the language of the arriving program. The currently supported MIME types are listed in Table 7.4. Note that browsers support multiple names for the languages, with or without the leading x. Mixing Local and Remote ScriptsA document can contain both local and remote SCRIPT declarations. This can make for sensible separation of program modules, with generic script libraries included via external files and documentspecific scripting placed within the documents. However, you cannot use the same SCRIPT element to simultaneously reference local and remote script components. To employ both external and internal scripts, you must use a different SCRIPT element for each.
Proposed Changes to SCRIPT: The TYPE AttributeThe World Wide Web Consortium recommends dropping the LANGUAGE attribute and replacing it by TYPE, the value of TYPE being the MIME type of the associated local or remote script. This makes SCRIPT consistent with other typelabeling mechanisms used on the Web, and eliminates the need to develop two different naming schemes (language names and MIME types) for the same things. As an example, a script element would take the form: <SCRIPT TYPE=text/javascript SRC=/path/to/prog.js></SCRIPT> This mechanism is supported by Netscape Navigator 4 and Internet Explorer 4. However, there is no defined mechanism for giving the language versionthe traditional way would be to include the version as a parameter, as in text/javascript; version=1.2 but this is not supported by either browser. Thus, for now, it is best to use both TYPE and LANGUAGE attributes, until proper support for TYPE is available.
|
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. |