scripts.html
html-spec/specifications/prose/html-4.01/interact/scripts.html · 849 line(s) · html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Scripts in HTML documents</title>
<link rel="previous" href="forms.html">
<link rel="next" href="../sgml/intro.html">
<link rel="contents" href="../cover.html#toc">
<link rel="stylesheet" type="text/css" href=
"http://www.w3.org/StyleSheets/TR/W3C-REC">
<link rel="STYLESHEET" href="../style/default.css" type="text/css">
</head>
<body>
<div class="navbar" align="center"> <a href="forms.html">previous</a>
<a href="../sgml/intro.html">next</a> <a href=
"../cover.html#minitoc">contents</a> <a href="../index/elements.html">
elements</a> <a href="../index/attributes.html">attributes</a> <a
href="../index/list.html">index</a>
<hr></div>
<h1 align="center"><a name="h-18">18</a> <a name="scripts">Scripts</a></h1>
<div class="subtoc">
<p><strong>Contents</strong></p>
<ol>
<li><a class="tocxref" href="#h-18.1">Introduction to scripts</a></li>
<li><a class="tocxref" href="#h-18.2">Designing documents for user agents that
support scripting</a>
<ol>
<li><a class="tocxref" href="#h-18.2.1">The <samp class="einst2">SCRIPT</samp>
element</a></li>
<li><a class="tocxref" href="#h-18.2.2">Specifying the scripting language</a>
<ul>
<li><a class="tocxref" href="#h-18.2.2.1">The default scripting
language</a></li>
<li><a class="tocxref" href="#h-18.2.2.2">Local declaration of a scripting
language</a></li>
<li><a class="tocxref" href="#h-18.2.2.3">References to HTML elements from a
script</a></li>
</ul>
</li>
<li><a class="tocxref" href="#h-18.2.3">Intrinsic events</a></li>
<li><a class="tocxref" href="#h-18.2.4">Dynamic modification of
documents</a></li>
</ol>
</li>
<li><a class="tocxref" href="#h-18.3">Designing documents for user agents that
don't support scripting</a>
<ol>
<li><a class="tocxref" href="#h-18.3.1">The <samp class="einst2">
NOSCRIPT</samp> element</a></li>
<li><a class="tocxref" href="#h-18.3.2">Hiding script data from user
agents</a></li>
</ol>
</li>
</ol>
</div>
<h2><a name="h-18.1">18.1</a> <span class="index-inst" title=
"script::introduction to"><a name="idx-script">Introduction to
scripts</a></span></h2>
A client-side <dfn>script</dfn> is a program that may accompany an HTML
document or be embedded directly in it. The program executes on the client's
machine when the document loads, or at some other time such as when a link is
activated. HTML's support for scripts is independent of the scripting language.
<p><span class="index-inst" title="script::uses of"><a name="idx-script-1">
Scripts</a></span> offer authors a means to extend HTML documents in highly
active and interactive ways. For example:</p>
<ul>
<li>Scripts may be evaluated as a document loads to modify the contents of the
document dynamically.</li>
<li>Scripts may accompany a form to process input as it is entered. Designers
may dynamically fill out parts of a form based on the values of other fields.
They may also ensure that input data conforms to predetermined ranges of
values, that fields are mutually consistent, etc.</li>
<li>Scripts may be triggered by events that affect the document, such as
loading, unloading, element focus, mouse movement, etc.</li>
<li>Scripts may be linked to form controls (e.g., buttons) to produce graphical
user interface elements.</li>
</ul>
<p>There are two types of scripts authors may attach to an HTML document:</p>
<ul>
<li><span class="index-inst" title="script::executed when document loaded"><a
name="idx-script-2">Those</a></span> that are executed one time when the
document is loaded by the user agent. Scripts that appear within a <a href=
"scripts.html#edef-SCRIPT" class="noxref"><samp class="einst">SCRIPT</samp></a>
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 <a
href="scripts.html#edef-NOSCRIPT" class="noxref"><samp class="einst">
NOSCRIPT</samp></a> element.</li>
<li><span class="index-inst" title="script::executed on event"><a name=
"idx-script-3">Those</a></span> that are executed every time a specific event
occurs. These scripts may be assigned to a number of elements via the <a href=
"#events">intrinsic event</a> attributes.</li>
</ul>
<div class="note">
<p><em><strong>Note.</strong> This specification includes more detailed
information about scripting in sections on <a href=
"../appendix/notes.html#notes-scripting">script macros</a>.</em></p>
</div>
<h2><a name="h-18.2">18.2</a> Designing documents for user agents that support
scripting</h2>
<p>The following sections discuss issues that concern user agents that support
scripting.</p>
<h3><a name="h-18.2.1">18.2.1</a> The <a name="edef-SCRIPT"><samp class="edef">
SCRIPT</samp></a> element</h3>
<div class="dtd-fragment">
<pre class="dtd-fragment">
<!ELEMENT <a href="scripts.html#edef-SCRIPT" class="noxref"><samp class=
"einst">SCRIPT</samp></a> - - <a href=
"../sgml/dtd.html#Script">%Script;</a> -- script statements -->
<!ATTLIST SCRIPT
<a href="../struct/links.html#adef-charset" class="noxref"><samp class=
"ainst-SCRIPT">charset</samp></a> <a href=
"../sgml/dtd.html#Charset">%Charset;</a> #IMPLIED -- char encoding of linked resource --
<a href="scripts.html#adef-type-SCRIPT" class="noxref"><samp class=
"ainst-SCRIPT">type</samp></a> <a href=
"../sgml/dtd.html#ContentType">%ContentType;</a> #REQUIRED -- content type of script language --
<a href="scripts.html#adef-src-SCRIPT" class="noxref"><samp class=
"ainst-SCRIPT">src</samp></a> <a href=
"../sgml/dtd.html#URI">%URI;</a> #IMPLIED -- URI for an external script --
<a href="scripts.html#adef-defer" class="noxref"><samp class=
"ainst-SCRIPT">defer</samp></a> (defer) #IMPLIED -- UA may defer execution of script --
>
</pre>
</div>
<p><em>Start tag: <strong>required</strong>, End tag: <strong>
required</strong></em></p>
<div class="adef-list">
<p><em>Attribute definitions</em></p>
<dl>
<dt><a name="adef-src-SCRIPT"><samp class="adef">src</samp></a> = <a href=
"../types.html#type-uri"><em>uri</em></a> <a href=
"../types.html#see-type-for-case">[CT]</a></dt>
<dd>This attribute specifies the location of an external script.</dd>
<dt><a name="adef-type-SCRIPT"><samp class="adef">type</samp></a> = <a href=
"../types.html#type-content-type"><em>content-type</em></a> <a href=
"../types.html#case-insensitive">[CI]</a></dt>
<dd>This attribute specifies the scripting language of the element's contents
and overrides the default scripting language. The scripting language is
specified as a content type (e.g., "text/javascript"). Authors must supply a
value for this attribute. There is no default value for this attribute.</dd>
<dt><a name="adef-language"><samp class="adef">language</samp></a> = <a href=
"../types.html#type-cdata"><em>cdata</em></a> <a href=
"../types.html#case-insensitive">[CI]</a></dt>
<dd><a href="../conform.html#deprecated"><strong>Deprecated.</strong></a> This
attribute specifies the scripting language of the contents of this element. Its
value is an identifier for the language, but since these identifiers are not
standard, this attribute has been <a href="../conform.html#deprecated">
deprecated</a> in favor of <samp class="ainst">type</samp>.</dd>
<dt><a name="adef-defer"><samp class="adef">defer</samp></a> <a href=
"../types.html#case-insensitive">[CI]</a></dt>
<dd>When set, this boolean attribute provides a hint to the user agent that the
script is not going to generate any document content (e.g., no "document.write"
in javascript) and thus, the user agent can continue parsing and
rendering.</dd>
</dl>
</div>
<div class="aref-list">
<p><em>Attributes defined elsewhere</em></p>
<ul>
<li><a href="../struct/links.html#adef-charset" class="noxref"><samp class=
"ainst">charset</samp></a>(<a href="../charset.html#doc-char-set">character
encodings</a>)</li>
</ul>
</div>
<p>The <a href="scripts.html#edef-SCRIPT" class="noxref"><samp class="einst">
SCRIPT</samp></a> element places a script within a document. This element may
appear any number of times in the <a href="../struct/global.html#edef-HEAD"
class="noxref"><samp class="einst">HEAD</samp></a> or <a href=
"../struct/global.html#edef-BODY" class="noxref"><samp class="einst">
BODY</samp></a> of an HTML document.</p>
<p>The script may be defined within the contents of the <samp class="einst">
SCRIPT</samp> element or in an external file. If the <a href=
"forms.html#adef-src" class="noxref"><samp class="ainst">src</samp></a>
attribute is not set, user agents must interpret the contents of the element as
the script. If the <a href="forms.html#adef-src" class="noxref"><samp class=
"ainst">src</samp></a> has a URI value, user agents must ignore the element's
contents and retrieve the script via the URI. Note that the <a href=
"../struct/links.html#adef-charset" class="noxref"><samp class="ainst">
charset</samp></a> attribute refers to the <a href="../charset.html">character
encoding</a> of the script designated by the <a href="forms.html#adef-src"
class="noxref"><samp class="ainst">src</samp></a> attribute; it does not
concern the content of the <a href="scripts.html#edef-SCRIPT" class="noxref">
<samp class="einst">SCRIPT</samp></a> element.</p>
<p>Scripts are evaluated by <em>script engines</em> that must be known to a
user agent.</p>
<p>The <a href="../types.html#type-script">syntax of script data</a> depends on
the scripting language.</p>
<h3><a name="h-18.2.2">18.2.2</a> <span class="index-inst" title="scripting
language::specification of|language::of script"><a name=
"idx-scripting_language">Specifying the scripting language</a></span></h3>
<p>As HTML does not rely on a specific scripting language, document authors
must explicitly tell user agents the language of each script. This may be done
either through a default declaration or a local declaration.</p>
<h4><span class="index-inst" title="default::scripting language|scripting
language::default"><a name="default-script">The default scripting
language</a></span> <a name="h-18.2.2.1"> </a></h4>
<p>Authors should specify the default scripting language for all scripts in a
document by including the following <a href="../struct/global.html#edef-META"
class="noxref"><samp class="einst">META</samp></a> declaration in the <a href=
"../struct/global.html#edef-HEAD" class="noxref"><samp class="einst">
HEAD</samp></a>:</p>
<pre>
<META http-equiv="Content-Script-Type" content="type">
</pre>
<p>where "type" is a <a href="../types.html#type-content-type">content type</a>
naming the scripting language. Examples of values include "text/tcl",
"text/javascript", "text/vbscript".</p>
<p><span class="index-inst" title="HTTP::Content-Script-Type
header|Content-Script-Type header">In the absence of a <a name="idx-HTTP" href=
"../struct/global.html#edef-META" class="noxref"><samp class="einst">
META</samp></a> declaration, the default can be set by a "Content-Script-Type"
HTTP header.</span></p>
<pre>
Content-Script-Type: type
</pre>
<p>where "type" is again a <a href="../types.html#type-content-type">content
type</a> naming the scripting language.</p>
<p>User agents should determine the default scripting language for a document
according to the following steps (highest to lowest priority):</p>
<ol>
<li>If any <a href="../struct/global.html#edef-META" class="noxref"><samp
class="einst">META</samp></a> declarations specify the "Content-Script-Type",
the last one in the character stream determines the default scripting
language.</li>
<li>Otherwise, if any HTTP headers specify the "Content-Script-Type", the last
one in the character stream determines the default scripting language.</li>
</ol>
<p>Documents that do not specify default scripting language information and
that contain elements that specify an <a href="#events">intrinsic event</a>
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.</p>
<h4><span class="index-inst" title="scripting language::local declaration"><a
name="idx-scripting_language-2">Local declaration of a scripting
language</a></span><a name="h-18.2.2.2"> </a></h4>
<p>The <samp class="ainst">type</samp> attribute must be specified for each <a
href="scripts.html#edef-SCRIPT" class="noxref"><samp class="einst">
SCRIPT</samp></a> element instance in a document. The value of the <samp class=
"ainst">type</samp> attribute for a <a href="scripts.html#edef-SCRIPT" class=
"noxref"><samp class="einst">SCRIPT</samp></a> element overrides the default
scripting language for that element.</p>
<div class="example">
<p>In this example, we declare the default scripting language to be "text/tcl".
We include one <a href="scripts.html#edef-SCRIPT" class="noxref"><samp class=
"einst">SCRIPT</samp></a> in the header, whose script is located in an external
file and is in the scripting language "text/vbscript". We also include one
<samp class="einst">SCRIPT</samp> in the body, which contains its own script
written in "text/javascript".</p>
<pre>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<TITLE>A document with SCRIPT</TITLE>
<META http-equiv="Content-Script-Type" content="text/tcl">
<SCRIPT type="text/vbscript" src="http://someplace.com/progs/vbcalc">
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT type="text/javascript">
<em>...some JavaScript...</em>
</SCRIPT>
</BODY>
</HTML>
</pre>
</div>
<h4><span class="index-inst" title="element::references from
scripts|script::references to elements"><a name="htmlrefs">References to HTML
elements from a script</a></span><a name="h-18.2.2.3"> </a></h4>
<p>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.</p>
<p>However, scripts should refer to an element according to its assigned name.
Scripting engines should observe the following precedence rules when
identifying an element: a <a href="../struct/links.html#adef-name-A" class=
"noxref"><samp class="ainst-A">name</samp></a> attribute takes precedence over
an <a href="../struct/global.html#adef-id" class="noxref"><samp class="ainst">
id</samp></a> if both are set. Otherwise, one or the other may be used.</p>
<h3><a name="h-18.2.3">18.2.3</a> <span class="index-inst" title=
"events|intrinsic events"><a name="events">Intrinsic events</a></span></h3>
<div class="note">
<p><em><strong>Note.</strong> 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 <a href=
"http://www.w3.org/">http://www.w3.org/</a> for more information).</em></p>
</div>
<div class="adef-list">
<p><em>Attribute definitions</em></p>
<dl>
<dt><a name="adef-onload"><samp class="adef">onload</samp></a> = <a href=
"../types.html#type-script"><em>script</em></a> <a href=
"../types.html#see-type-for-case">[CT]</a></dt>
<dd>The <samp>onload</samp> event occurs when the user agent finishes loading a
window or all frames within a <a href="../present/frames.html#edef-FRAMESET"
class="noxref"><samp class="einst">FRAMESET</samp></a>. This attribute may be
used with <a href="../struct/global.html#edef-BODY" class="noxref"><samp class=
"einst">BODY</samp></a> and <a href="../present/frames.html#edef-FRAMESET"
class="noxref"><samp class="einst">FRAMESET</samp></a> elements.</dd>
<dt><a name="adef-onunload"><samp class="adef">onunload</samp></a> = <a href=
"../types.html#type-script"><em>script</em></a> <a href=
"../types.html#see-type-for-case">[CT]</a></dt>
<dd>The <samp>onunload</samp> event occurs when the user agent removes a
document from a window or frame. This attribute may be used with <a href=
"../struct/global.html#edef-BODY" class="noxref"><samp class="einst">
BODY</samp></a> and <a href="../present/frames.html#edef-FRAMESET" class=
"noxref"><samp class="einst">FRAMESET</samp></a> elements.</dd>
<dt><a name="adef-onclick"><samp class="adef">onclick</samp></a> = <a href=
"../types.html#type-script"><em>script</em></a> <a href=
"../types.html#see-type-for-case">[CT]</a></dt>
<dd>The <samp>onclick</samp> event occurs when the pointing device button is
clicked over an element. This attribute may be used with most elements.</dd>
<dt><a name="adef-ondblclick"><samp class="adef">ondblclick</samp></a> = <a
href="../types.html#type-script"><em>script</em></a> <a href=
"../types.html#see-type-for-case">[CT]</a></dt>
<dd>The <samp>ondblclick</samp> event occurs when the pointing device button is
double clicked over an element. This attribute may be used with most
elements.</dd>
<dt><a name="adef-onmousedown"><samp class="adef">onmousedown</samp></a> = <a
href="../types.html#type-script"><em>script</em></a> <a href=
"../types.html#see-type-for-case">[CT]</a></dt>
<dd>The <samp>onmousedown</samp> event occurs when the pointing device button
is pressed over an element. This attribute may be used with most elements.</dd>
<dt><a name="adef-onmouseup"><samp class="adef">onmouseup</samp></a> = <a href=
"../types.html#type-script"><em>script</em></a> <a href=
"../types.html#see-type-for-case">[CT]</a></dt>
<dd>The <samp>onmouseup</samp> event occurs when the pointing device button is
released over an element. This attribute may be used with most elements.</dd>
<dt><a name="adef-onmouseover"><samp class="adef">onmouseover</samp></a> = <a
href="../types.html#type-script"><em>script</em></a> <a href=
"../types.html#see-type-for-case">[CT]</a></dt>
<dd>The <samp>onmouseover</samp> event occurs when the pointing device is moved
onto an element. This attribute may be used with most elements.</dd>
<dt><a name="adef-onmousemove"><samp class="adef">onmousemove</samp></a> = <a
href="../types.html#type-script"><em>script</em></a> <a href=
"../types.html#see-type-for-case">[CT]</a></dt>
<dd>The <samp>onmousemove</samp> event occurs when the pointing device is moved
while it is over an element. This attribute may be used with most
elements.</dd>
<dt><a name="adef-onmouseout"><samp class="adef">onmouseout</samp></a> = <a
href="../types.html#type-script"><em>script</em></a> <a href=
"../types.html#see-type-for-case">[CT]</a></dt>
<dd>The <samp>onmouseout</samp> event occurs when the pointing device is moved
away from an element. This attribute may be used with most elements.</dd>
<dt><a name="adef-onfocus"><samp class="adef">onfocus</samp></a> = <a href=
"../types.html#type-script"><em>script</em></a> <a href=
"../types.html#see-type-for-case">[CT]</a></dt>
<dd>The <a href="scripts.html#adef-onfocus" class="noxref"><samp class="ainst">
onfocus</samp></a> event occurs when an element receives focus either by the
pointing device or by tabbing navigation. This attribute may be used with the
following elements: <a href="../struct/links.html#edef-A" class="noxref"><samp
class="einst">A</samp></a>, <a href="../struct/objects.html#edef-AREA" class=
"noxref"><samp class="einst">AREA</samp></a>, <a href="forms.html#edef-LABEL"
class="noxref"><samp class="einst">LABEL</samp></a>, <a href=
"forms.html#edef-INPUT" class="noxref"><samp class="einst">INPUT</samp></a>, <a
href="forms.html#edef-SELECT" class="noxref"><samp class="einst">
SELECT</samp></a>, <a href="forms.html#edef-TEXTAREA" class="noxref"><samp
class="einst">TEXTAREA</samp></a>, and <a href="forms.html#edef-BUTTON" class=
"noxref"><samp class="einst">BUTTON</samp></a>.</dd>
<dt><a name="adef-onblur"><samp class="adef">onblur</samp></a> = <a href=
"../types.html#type-script"><em>script</em></a> <a href=
"../types.html#see-type-for-case">[CT]</a></dt>
<dd>The <samp>onblur</samp> event occurs when an element loses focus either by
the pointing device or by tabbing navigation. It may be used with the same
elements as <samp>onfocus</samp>.</dd>
<dt><a name="adef-onkeypress"><samp class="adef">onkeypress</samp></a> = <a
href="../types.html#type-script"><em>script</em></a> <a href=
"../types.html#see-type-for-case">[CT]</a></dt>
<dd>The <samp>onkeypress</samp> event occurs when a key is pressed and released
over an element. This attribute may be used with most elements.</dd>
<dt><a name="adef-onkeydown"><samp class="adef">onkeydown</samp></a> = <a href=
"../types.html#type-script"><em>script</em></a> <a href=
"../types.html#see-type-for-case">[CT]</a></dt>
<dd>The <samp>onkeydown</samp> event occurs when a key is pressed down over an
element. This attribute may be used with most elements.</dd>
<dt><a name="adef-onkeyup"><samp class="adef">onkeyup</samp></a> = <a href=
"../types.html#type-script"><em>script</em></a> <a href=
"../types.html#see-type-for-case">[CT]</a></dt>
<dd>The <samp>onkeyup</samp> event occurs when a key is released over an
element. This attribute may be used with most elements.</dd>
<dt><a name="adef-onsubmit"><samp class="adef">onsubmit</samp></a> = <a href=
"../types.html#type-script"><em>script</em></a> <a href=
"../types.html#see-type-for-case">[CT]</a></dt>
<dd>The <samp>onsubmit</samp> event occurs when a form is submitted. It only
applies to the <a href="forms.html#edef-FORM" class="noxref"><samp class=
"einst">FORM</samp></a> element.</dd>
<dt><a name="adef-onreset"><samp class="adef">onreset</samp></a> = <a href=
"../types.html#type-script"><em>script</em></a> <a href=
"../types.html#see-type-for-case">[CT]</a></dt>
<dd>The <samp>onreset</samp> event occurs when a form is reset. It only applies
to the <a href="forms.html#edef-FORM" class="noxref"><samp class="einst">
FORM</samp></a> element.</dd>
<dt><a name="adef-onselect"><samp class="adef">onselect</samp></a> = <a href=
"../types.html#type-script"><em>script</em></a> <a href=
"../types.html#see-type-for-case">[CT]</a></dt>
<dd>The <samp>onselect</samp> event occurs when a user selects some text in a
text field. This attribute may be used with the <a href="forms.html#edef-INPUT"
class="noxref"><samp class="einst">INPUT</samp></a> and <a href=
"forms.html#edef-TEXTAREA" class="noxref"><samp class="einst">
TEXTAREA</samp></a> elements.</dd>
<dt><a name="adef-onchange"><samp class="adef">onchange</samp></a> = <a href=
"../types.html#type-script"><em>script</em></a> <a href=
"../types.html#see-type-for-case">[CT]</a></dt>
<dd>The <samp>onchange</samp> event occurs when a control loses the input focus
<em>and</em> its value has been modified since gaining focus. This attribute
applies to the following elements: <a href="forms.html#edef-INPUT" class=
"noxref"><samp class="einst">INPUT</samp></a>, <a href="forms.html#edef-SELECT"
class="noxref"><samp class="einst">SELECT</samp></a>, and <a href=
"forms.html#edef-TEXTAREA" class="noxref"><samp class="einst">
TEXTAREA</samp></a>.</dd>
</dl>
</div>
<p>It is possible to associate an action with a certain number of events that
occur when a user interacts with a user agent. Each of the "intrinsic events"
listed above takes a value that is a script. The script is executed whenever
the event occurs for that element. The <a href="../types.html#type-script">
syntax of script data</a> depends on the scripting language.</p>
<p><span class="index-inst" title="control::events for"><a name="idx-control">
Control elements</a></span> such as <a href="forms.html#edef-INPUT" class=
"noxref"><samp class="einst">INPUT</samp></a>, <a href="forms.html#edef-SELECT"
class="noxref"><samp class="einst">SELECT</samp></a>, <a href=
"forms.html#edef-BUTTON" class="noxref"><samp class="einst">BUTTON</samp></a>,
<a href="forms.html#edef-TEXTAREA" class="noxref"><samp class="einst">
TEXTAREA</samp></a>, and <a href="forms.html#edef-LABEL" class="noxref"><samp
class="einst">LABEL</samp></a> all respond to certain intrinsic events. When
these elements do not appear within a form, they may be used to augment the
graphical user interface of the document.</p>
<p>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.</p>
<p>The following examples show some possible control and user interface
behavior based on intrinsic events.</p>
<div class="example">
<p>In the following example, userName is a required text field. When a user
attempts to leave the field, the <samp>onblur</samp> event calls a JavaScript
function to confirm that userName has an acceptable value.</p>
<pre>
<INPUT NAME="userName" onblur="validUserName(this.value)">
</pre>
<p>Here is another JavaScript example:</p>
<pre>
<INPUT NAME="num"
onchange="if (!checkNum(this.value, 1, 10))
{this.focus();this.select();} else {thanks()}"
VALUE="0">
</pre>
</div>
<div class="example">
<p>Here is a VBScript example of an event handler for a text field:</p>
<pre>
<INPUT name="edit1" size="50">
<SCRIPT type="text/vbscript">
Sub edit1_changed()
If edit1.value = "abc" Then
button1.enabled = True
Else
button1.enabled = False
End If
End Sub
</SCRIPT>
</pre>
<p>Here is the same example using Tcl:</p>
<pre>
<INPUT name="edit1" size="50">
<SCRIPT type="text/tcl">
proc edit1_changed {} {
if {[edit value] == abc} {
button1 enable 1
} else {
button1 enable 0
}
}
edit1 onChange edit1_changed
</SCRIPT>
</pre>
</div>
<div class="example">
<p>Here is a JavaScript example for event binding within a script. First,
here's a simple click handler:</p>
<pre>
<BUTTON type="button" name="mybutton" value="10">
<SCRIPT type="text/javascript">
function my_onclick() {
. . .
}
document.form.mybutton.onclick = my_onclick
</SCRIPT>
</BUTTON>
</pre>
<p>Here's a more interesting window handler:</p>
<pre>
<SCRIPT type="text/javascript">
function my_onload() {
. . .
}
var win = window.open("some/other/URI")
if (win) win.onload = my_onload
</SCRIPT>
</pre>
<p>In Tcl this looks like:</p>
<pre>
<SCRIPT type="text/tcl">
proc my_onload {} {
. . .
}
set win [window open "some/other/URI"]
if {$win != ""} {
$win onload my_onload
}
</SCRIPT>
</pre>
</div>
<div class="note">
<p><em>Note that "document.write" or equivalent statements in intrinsic event
handlers create and write to a new document rather than modifying the current
one.</em></p>
</div>
<h3><a name="h-18.2.4">18.2.4</a> <span class="index-inst" title=
"document::dynamic modification with script|script::used to modify document"><a
name="idx-document">Dynamic modification of documents</a></span></h3>
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).
<p>The dynamic modification of a document may be modeled as follows:</p>
<ol>
<li>All <a href="scripts.html#edef-SCRIPT" class="noxref"><samp class="einst">
SCRIPT</samp></a> elements are evaluated in order as the document is
loaded.</li>
<li>All script constructs within a given <a href="scripts.html#edef-SCRIPT"
class="noxref"><samp class="einst">SCRIPT</samp></a> element that generate SGML
CDATA are evaluated. Their combined generated text is inserted in the document
in place of the <a href="scripts.html#edef-SCRIPT" class="noxref"><samp class=
"einst">SCRIPT</samp></a> element.</li>
<li>The generated CDATA is re-evaluated.</li>
</ol>
<p>HTML documents are constrained to conform to the HTML DTD both before and
after processing any <a href="scripts.html#edef-SCRIPT" class="noxref"><samp
class="einst">SCRIPT</samp></a> elements.</p>
<div class="example">
<p>The following example illustrates how scripts may modify a document
dynamically. The following script:</p>
<pre>
<TITLE>Test Document</TITLE>
<SCRIPT type="text/javascript">
document.write("<p><b>Hello World!<\/b>")
</SCRIPT>
</pre>
<p>Has the same effect as this HTML markup:</p>
<pre>
<TITLE>Test Document</TITLE>
<P><B>Hello World!</B>
</pre>
</div>
<h2><a name="h-18.3">18.3</a> <span class="index-inst" title="script::when
unsupported"><a name="idx-script-6">Designing documents for user agents that
don't support scripting</a></span></h2>
<p>The following sections discuss how authors may create documents that work
for user agents that don't support scripting.</p>
<h3><a name="h-18.3.1">18.3.1</a> The <a name="edef-NOSCRIPT"><samp class=
"edef">NOSCRIPT</samp></a> element</h3>
<div class="dtd-fragment">
<pre class="dtd-fragment">
<!ELEMENT <a href="scripts.html#edef-NOSCRIPT" class="noxref"><samp class=
"einst">NOSCRIPT</samp></a> - - (<a href="../sgml/dtd.html#block">%block;</a>)+
-- alternate content container for non script-based rendering -->
<!ATTLIST NOSCRIPT
<a href=
"../sgml/dtd.html#attrs">%attrs;</a> -- <a href=
"../sgml/dtd.html#coreattrs">%coreattrs</a>, <a href=
"../sgml/dtd.html#i18n">%i18n</a>, <a href=
"../sgml/dtd.html#events">%events</a> --
>
</pre>
</div>
<p><em>Start tag: <strong>required</strong>, End tag: <strong>
required</strong></em></p>
The <a href="scripts.html#edef-NOSCRIPT" class="noxref"><samp class="einst">
NOSCRIPT</samp></a> element allows authors to provide alternate content when a
script is not executed. The content of a <a href="scripts.html#edef-NOSCRIPT"
class="noxref"><samp class="einst">NOSCRIPT</samp></a> element should only be
rendered by a script-aware user agent in the following cases:
<ul>
<li>The user agent is configured not to evaluate scripts.</li>
<li>The user agent doesn't support a scripting language invoked by a <a href=
"scripts.html#edef-SCRIPT" class="noxref"><samp class="einst">SCRIPT</samp></a>
element earlier in the document.</li>
</ul>
<p>User agents that do not support client-side scripts must render this
element's contents.</p>
<div class="example">
<p>In the following example, a user agent that executes the <a href=
"scripts.html#edef-SCRIPT" class="noxref"><samp class="einst">SCRIPT</samp></a>
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.</p>
<pre>
<SCRIPT type="text/tcl">
<em>...some Tcl script to insert data...</em>
</SCRIPT>
<NOSCRIPT>
<P>Access the <A href="http://someplace.com/data">data.</A>
</NOSCRIPT>
</pre>
</div>
<h3><a name="h-18.3.2">18.3.2</a> <span class="index-inst" title="user
agent::and script data|comments::used to hide script data|script::comments to
hide"><a name="idx-user_agent">Hiding script data from user
agents</a></span></h3>
<p>User agents that don't recognize the <a href="scripts.html#edef-SCRIPT"
class="noxref"><samp class="einst">SCRIPT</samp></a> 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 <a href=
"scripts.html#edef-SCRIPT" class="noxref"><samp class="einst">SCRIPT</samp></a>
element will thus ignore the comment while smart scripting engines will
understand that the script in comments should be executed.</p>
<p>Another solution to the problem is to keep scripts in external documents and
refer to them with the <a href="forms.html#adef-src" class="noxref"><samp
class="ainst">src</samp></a> attribute.</p>
<div class="example">
<p><strong>Commenting scripts in JavaScript</strong>
<br>
The JavaScript engine allows the string "<!--" to occur at the start of a
SCRIPT element, and ignores further characters until the end of the line.
JavaScript interprets "//" as starting a comment extending to the end of the
current line. This is needed to hide the string "-->" from the JavaScript
parser.</p>
<pre>
<SCRIPT type="text/javascript">
<!-- to hide script contents from old browsers
function square(i) {
document.write("The call passed ", i ," to the function.","<BR>")
return i * i
}
document.write("The function returned ",square(5),".")
// end hiding contents from old browsers -->
</SCRIPT>
</pre>
</div>
<div class="example">
<p><strong>Commenting scripts in VBScript</strong>
<br>
In VBScript, a single quote character causes the rest of the current line to
be treated as a comment. It can therefore be used to hide the string "-->"
from VBScript, for instance:</p>
<pre>
<SCRIPT type="text/vbscript">
<!--
Sub foo()
...
End Sub
' -->
</SCRIPT>
</pre>
</div>
<div class="example">
<p><strong>Commenting scripts in TCL</strong>
<br>
In Tcl, the "#" character comments out the rest of the line:</p>
<pre>
<SCRIPT type="text/tcl">
<!-- to hide script contents from old browsers
proc square {i} {
document write "The call passed $i to the function.<BR>"
return [expr $i * $i]
}
document write "The function returned [square 5]."
# end hiding contents from old browsers -->
</SCRIPT>
</pre>
</div>
<div class="note">
<p><em><strong>Note.</strong> Some browsers close comments on the first ">"
character, so to hide script content from such browsers, you can transpose
operands for relational and shift operators (e.g., use "y < x" rather than
"x > y") or use scripting language-dependent escapes for ">".</em></p>
</div>
<div class="navbar" align="center">
<hr><a href="forms.html">previous</a> <a href="../sgml/intro.html">
next</a> <a href="../cover.html#minitoc">contents</a> <a href=
"../index/elements.html">elements</a> <a href=
"../index/attributes.html">attributes</a> <a href="../index/list.html">
index</a></div>
</body>
</html>