script

< html‎ | elements
Jump to: navigation, search

script



Summary

The script element enables dynamic script and data blocks to be included in documents.


Overview Table

DOM Interface HTMLScriptElement

Examples

The following code snippet provides a scenario where an error occurs.

<html>
<head>
<SCRIPT LANGUAGE="XML" id="mySrc1">
<offerings>
 <class><materials>This should render.</materials><time>1.5
hr</time></class>
</offerings>
</SCRIPT>
<SCRIPT LANGUAGE="javascript">
function returnIslandRootName()
{
  var islandRoot = document.all["mySrc1"].XMLDocument;
  console.log(islandRoot.nodeName);
}
</SCRIPT>
</head>
<body>
<button onclick="returnIslandRootName()">Test the XML Data Island</button>
</body>
</html>

Because the XML data island is the first instance of the script object that has the language attribute defined, MSHTML attempts to locate the returnIslandRootName function in the XML and fails. To correct the sample, the order of the script objects can change, as shown by the following example:

<html>
<head>
<SCRIPT LANGUAGE="javascript">
function returnIslandRootName()
{
  var islandRoot = document.all["mySrc1"].XMLDocument;
  console.log(islandRoot.nodeName);
}
</SCRIPT>
<SCRIPT LANGUAGE="XML" id="mySrc1">
<offerings>
 <class><materials>This should render.</materials><time>1.5
hr</time></class>
</offerings>
</SCRIPT>
</head>
<body>
<button onclick="returnIslandRootName()">Test the XML Data Island</button>
</body>
</html>

Notes

Remarks

Code within the script block that is not contained within a function is executed immediately as the document is loaded. To keep scripts from being displayed, nest the script block within a comment block. Script appearing after a frameSet element is ignored. Whenever the language attribute is not defined on the script object, then MSHTML attempts to select a suitable scripting engine. An error generally occurs if the wrong scripting engine is selected. When more than one script object is used in a document, it can be necessary to specify the language attribute for each script object, and doing so is always recommended. The order of the script objects in a document can also be important, especially if scripting event handlers are assigned to one or more elements in the document. XML is legitimate content for the script object, but XML is not a scripting language. Therefore, an error can occur if MSHTML selects an XML data island as the script object that contains an event handler function. This can happen because MSHTML selects the first script object that has the language attribute defined as the default script block for event handlers. For more information, see the examples. Windows Internet Explorer 8 and later. The value of the src attribute depends on the current document compatibility mode.

Standards information


Members

The script object has these types of members:

  • [#events Events]
  • [#methods Methods]
  • [#properties Properties]


Events

The script object has these events. {

Compatibility

Desktop

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support ? ? ? ? ?

Mobile

Feature Android BlackBerry Chrome for mobile Firefox Mobile (Gecko) IE Mobile Opera Mobile Opera Mini Safari Mobile
Basic support ? ? ? ? ? ? ? ?

See also

Related pages (MSDN)

  • XML Data Islands

This article contains content originally from external sources.

Portions of this content come from the Microsoft Developer Network: [Windows Internet Explorer API reference Article]