18. XHTML Scripting Module


This section is normative.

The Scripting Module defines elements that are used to contain information pertaining to executable scripts or the lack of support for executable scripts. Elements and attributes included in this module are:

Elements Attributes Minimal Content Model
noscript Common (Heading | List | Block)+
script charset (Charset), defer ("defer"), src (URI), type* (ContentType), xml:space="preserve" PCDATA

When this module is used, the script and noscript elements are added to the Block and Inline content sets of the Text Module. In addition, the script element is added to the content model of the head element defined in the Structure Module.

Implementation: DTD

18.1. The noscript element


The Core collection
A collection of basic attributes used on all elements, including class, id, title.
The I18N collection
A collection of attributes related to Internationalization, including the xml:lang.

The noscript element allows authors to provide alternate content when a script is not executed. The content of a noscript element should only be rendered by a script-aware user agent in the following cases:

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...
 <p>Access the <a href="http://someplace.com/data">data.</a></p>

18.2. The script element


src = URI
This attribute specifies the location of an external source for the contents of the element.
type = ContentType
This attribute gives an advisory hint as to the content type of the content available at the link target address. It allows user agents to opt to use a fallback mechanism rather than fetch the content if they are advised that they will get content in a content type they do not support.

Authors who use this attribute take responsibility to manage the risk that it may become inconsistent with the content available at the link target address.

For the current list of registered content types, please consult [MIMETYPES].

Attribute defer not found

The attribute defer was not able to be loaded by the document generator. This implies that either 1) the attribute is not yet defined, or 2) the name is incorrect.
charset = Charset
This attribute specifies the character encoding of the resource designated by the link. Please consult the section on character encodings for more details.

The script element places a script within a document. This element may appear any number of times in the head or body of an XHTML 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.

The syntax of script data depends on the scripting language.

18.2.1. Specifying the scripting language

As XHTML 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.

18.2.2. Declaration of a scripting language

The type attribute must be specified for each script element instance in a document.

In this example, we include one script in the header, whose script is located in an external file and is in the scripting language "text/vbscript". We also include one script in the body, which contains its own script written in "text/javascript".

<html xmlns="http://www.w3.org/2002/06/xhtml2">
<title>A document with script</title>
<script type="text/vbscript" src="http://someplace.com/progs/vbcalc"/>
<script type="text/javascript">
...some JavaScript...

18.2.3. References to XHTML elements from a script

Each scripting language has its own conventions for referring to XHTML objects from within a script. This specification does not define a standard mechanism for referring to XHTML objects.

18.2.4. Dynamic modification of documents

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 XHTML object model supported by some vendors).

The dynamic modification of a document may be modeled as follows:

  1. All script elements are evaluated in order as the document is loaded.
  2. All script constructs within a given script element that generate CDATA are evaluated. Their combined generated text is inserted in the document directly after the script element.
  3. The generated CDATA is re-evaluated.

XHTML documents are constrained to conform to the XHTML DTD both before and after processing any script elements.

The following example illustrates how scripts may modify a document dynamically. The following script:

 <title>Test Document</title>
 <script type="text/javascript">
     document.write("<p><b>Hello World!<\/b><\/p>")

Has the same effect as this markup:

 <title>Test Document</title>
 <p><b>Hello World!</b></p>