16. XHTML Scripting Module

Contents

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), declare ("declare"), src (URI), type* (ContentType), xml:space="preserve" PCDATA | script | noscript

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

Implementation: RELAX NG

16.1. The noscript element

Attributes

The Common collection
A collection of other attribute collections, including: Core, Events, I18N, Bi-directional, Edit, Embedding, Map, and Hypertext

The noscript element allows authors to provide alternate content when a script is not executed. The content of a noscript element will be rendered if and only if the containing script is not processed. noscript elements that are not contained in a script element will only be rendered 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" src="http://example.org/script">
 <noscript>
  <p>Access the <a href="http://example.org/data">data.</a></p>
 </noscript>
</script>

16.2. The script element

Attributes

The Embedding collection
A collection of attributes related to embedding content, including src and type.
declare = "declare"
When present, this boolean attribute makes the current object element a declaration only - not one that is to be executed until after the document has completed loading and has been called through a user event such as selecting an anchor that references the object.
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.

16.2.1. Rules for processing scripts

Scripts are evaluated by script engines that must be known to a user agent.

A user agent must interpret a script element according to the following precedence rules:

  1. The user agent must first try to process the script element, but not the embedded content.
  2. If the user agent is not able to process the script for any reason (configured not to, no support of the scripting language, no access to an external resource, etc.), it must try to process its contents.
  3. If the content is inline text it must be evaluated as script data of the scripting language of the containing script element.
  4. If the content is a noscript element, its content should be rendered.
  5. If the content is a script element, its content should be processed according to these rules.

The syntax of script data depends on the scripting language.

16.2.2. 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.

16.2.3. 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". The JavaScript code contained in the inner script will be evaluated if and only if the user agent isn't evaluating the outer script. We also include one script in the body, which contains its own script written in "text/x-perl".

<html xmlns="http://www.w3.org/2002/06/xhtml2">
 <head>
  <title>A document with script</title>
  <script type="text/x-vbscript" src="http://example.org/progs/vbcalc">
   <script type="text/javascript">
    ...some inline JavaScript...
   </script>
  </script>
 </head>
 <body>
    <script type="text/x-perl"/>
 </body>
</html>

16.2.4. Dynamic modification of documents

Note that because of the XML processing model, where a document is first parsed before being processed, the form of dynamic generation used in earlier versions of HTML, using document.write cannot be used in XHTML2. To dynamically generate content in XHTML you have to add elements to the DOM tree using DOM calls [DOM] rather than using document.write top generate text that then gets parsed.