element and how to specify the scripting language. It also covers intrinsic events that can trigger scripts, such as onclick, onload, and onmouseover. Scripts allow authors to make documents highly interactive and dynamic by modifying content as it loads or in response to user input and events."> element and how to specify the scripting language. It also covers intrinsic events that can trigger scripts, such as onclick, onload, and onmouseover. Scripts allow authors to make documents highly interactive and dynamic by modifying content as it loads or in response to user input and events.">
18.1 Introduction To Scripts
18.1 Introduction To Scripts
18 Scripts
Contents
1. Introduction to scripts
2. Designing documents for user agents that support scripting
1. The SCRIPT element
2. Specifying the scripting language
The default scripting language
Local declaration of a scripting language
References to HTML elements from a script
3. Intrinsic events
4. Dynamic modification of documents
3. Designing documents for user agents that don't support scripting
1. The NOSCRIPT element
2. Hiding script data from user agents
Scripts offer authors a means to extend HTML documents in highly active and
interactive ways. For example:
Those that are executed one time when the document is loaded by the
user agent. Scripts that appear within a SCRIPT element are executed
when the document is loaded. For user agents that cannot or will not
handle scripts, authors may include alternate content via
the NOSCRIPT element.
Those that are executed every time a specific event occurs. These
scripts may be assigned to a number of elements via the intrinsic
event attributes.
Attribute definitions
charset(character encodings)
The SCRIPT element places a script within a document. This element may
appear any number of times in the HEAD or BODY of an HTML document.
The script may be defined within the contents of the SCRIPT element or in an
external file. If the src attribute is not set, user agents must interpret the
contents of the element as the script. If the src has a URI value, user agents
must ignore the element's contents and retrieve the script via the URI. Note
that the charset attribute refers to the character encoding of the script
designated by the src attribute; it does not concern the content of
the SCRIPT element.
Scripts are evaluated by script engines that must be known to a user agent.
Authors should specify the default scripting language for all scripts in a
document by including the following META declaration in the HEAD:
<META http-equiv="Content-Script-Type" content="type">
where "type" is a content type naming the scripting language. Examples of
values include "text/tcl", "text/javascript", "text/vbscript".
User agents should determine the default scripting language for a document
according to the following steps (highest to lowest priority):
Documents that do not specify default scripting language information and that
contain elements that specify an intrinsic event script are incorrect. User
agents may still attempt to interpret incorrectly specified scripts but are not
required to. Authoring tools should generate default scripting language
information to help authors avoid creating incorrect documents.
The type attribute must be specified for each SCRIPT element instance in a
document. The value of the type attribute for a SCRIPT element overrides
the default scripting language for that element.
Each scripting language has its own conventions for referring to HTML objects
from within a script. This specification does not define a standard mechanism
for referring to HTML objects.
Note. Authors of HTML documents are advised that changes are likely to
occur in the realm of intrinsic events (e.g., how scripts are bound to events).
Research in this realm is carried on by members of the W3C Document
Object Model Working Group (see the W3C Web Site
at http://www.w3.org/ for more information).
Attribute definitions
For instance, authors may want to include press buttons in their documents
that do not submit a form but still communicate with a server when they are
activated.
The following examples show some possible control and user interface
behavior based on intrinsic events.
Here is a JavaScript example for event binding within a script. First, here's a
simple click handler:
<SCRIPT type="text/javascript">
function my_onload() {
. . .
}
var win = window.open("some/other/URI")
if (win) win.onload = my_onload
</SCRIPT>
Scripts that are executed when a document is loaded may be able to modify
the document's contents dynamically. The ability to do so depends on the
scripting language itself (e.g., the "document.write" statement in the HTML
object model supported by some vendors).
HTML documents are constrained to conform to the HTML DTD both before
and after processing any SCRIPT elements.
User agents that do not support client-side scripts must render this element's
contents.
In the following example, a user agent that executes the SCRIPT will include
some dynamically created data in the document. If the user agent doesn't
support scripts, the user may still retrieve the data through a link.
<SCRIPT type="text/tcl">
...some Tcl script to insert data...
</SCRIPT>
<NOSCRIPT>
<P>Access the <A href="http://someplace.com/data">data.</A>
</NOSCRIPT>
User agents that don't recognize the SCRIPT element will likely render that
element's contents as text. Some scripting engines, including those for
languages JavaScript, VBScript, and Tcl allow the script statements to be
enclosed in an SGML comment. User agents that don't recognize
the SCRIPT element will thus ignore the comment while smart scripting
engines will understand that the script in comments should be executed.