Next / Previous / Contents / TCC Help System / NM Tech homepage

8.7. The script element: Including executable code

A script is an executable program attached to your Web page. There are two ways to attach a script:

  1. To execute the script when the page is loaded, include the program in a script element.

  2. You can also set up a script that will be executed when the user performs certain operations, like clicking or moving a mouse over an element. For this form of scripting, see Section 16, “Event attributes”.

There is no guarantee your script will execute. In some browsing environments, scripts are disabled. Furthermore, not all languages are supported. To provide substitute content in these cases, see Section 8.8, “The noscript element: What to do when your script can't be run”.

Assuming that scripting is allowed in your chosen language, any output generated by your script will replace the script element before the page is rendered. For example, here is a complete page that uses Javascript to generate the page dynamically:

<html xmlns="" xml:lang="en">
    <title>This title will be displayed</title>
    <script type="text/javascript">
       document.write ( "<p>Javascript succeeded.</p>" )
       <p>Javascript failed.</p>

If the above page is loaded in an environment that allows Javascript, the page will display the paragraph “Javascript succeeded.”; if the script can't be run, it will display the paragraph “Javascript failed.”

Here is the content model:

element script
  attribute type { text },
  attribute src { xsd:anyURI }?,
  attribute charset { text }?,
  attribute xml:space { "preserve" }?,

This required attribute must specify the kind of script being executed. The value is a MIME type; see Section 6.7, “MIME types: Defining a resource's format”.


There are two places you can put the actual code of your script. You can include it directly as the content of the script element. Alternately, you can place the script in a separate file and use the src attribute to point at the script's URI. Here's an example:

  <script type="text/intercal" src="ical/ignatz.ic"/>


If you use the src attribute to invoke a script outside the current document, and that script is in a different character set, you can use the charset attribute to specify that character set. See Section 15.2, “The charset attribute: Declaring a character set”.


If whitespace is significant in your scripting language, use the xml:space="preserve" attribute.

Unless you are using the src attribute to point at an external script, place your script between the <script> and </script> tags.