script element | script object
[This documentation is preliminary and is subject to change.]
Specifies a script for the document that is interpreted by a script engine.
![]() ![]() |
Standards information
- Document Object Model (DOM) Level 2 HTML Specification, Section 1.6.5
- HTML 4.01 Specification, Section 18.2.1
DOM Information
Inheritance Hierarchy
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.
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>
See also
Build date: 3/8/2012
Here is the test page
<head>
<script language="JavaScript" type="text/javascript" src="test.js" onload="alert('scripLoader.js loaded')"></script>
<script language="JavaScript" onload="alert('on-line script loaded')">
<!--
function load()
{
alert('Hello World!');
}
function scriptLoaded(o) { msg.innerHTML += 'script ' + o.src + ' loaded.'; }
//-->
</script>
</head>
<body onload="load()">
<span id='msg'></span>
</body>
<script language="JavaScript" type="text/javascript" src="test.js" onload="alert('test.js from page loaded')"></script>
</html>
- 12/10/2010
- ivostoykov
<head>
<script type="text/javascript" language="Javascript">
function islandRootName(){
var islandRoot = document.getElementById('mySrc1').XMLDocument;
alert(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="islandRootName();">Test the XML Data Island</button>
</body>
</html>
- 7/28/2009
- unique_username
COMMENT: <script> is oft part of the HEAD:-- It works in BODY, but old w3-browsers render it;
The current w3.org 'speccommendation' for SCRIPT in HTML BODY,
<!-- suggests commenting but is readily breakable> ... It is not ready for primetime HTML -->
- 12/4/2008
- Mr. Raymond Kenneth Petry
- 12/7/2008
- Thomas Lee

