13 out of 26 rated this helpful - Rate this topic

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.

Document Object Model (DOM) Level 2 HTML Specification, Section 1.6.5HTML 4.01 Specification, Section 18.2.1

Standards information

DOM Information

Inheritance Hierarchy

 Node
  Element
   HTMLElement
     script

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

XML Data Islands

 

 

Build date: 3/8/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
onload Event
onload Event does not works in IE 8.0.7600.16385

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>

revised code sample to reflect current standards
<html>
<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>
caution - hiding script

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