HTML元素 - script

定义

指定由脚本引擎解释的页面中的脚本。

Specifies a script for the page that is interpreted by a script engine.

注释

SCRIPT 块内的代码如果不包含在函数内,那么将会在页面载入时立即执行。要保证脚本不在较低版本的浏览器中显示,应该将 SCRIPT 块嵌套在 COMMENT 块内。

FRAMESET 元素后出现的脚本将被忽略。

此元素在 Internet Explorer 3.0 及以上版本的 HTML 中可用,在 Internet Explorer 4.0 及以上版本的脚本中可用。

只要没有在 SCRIPT 对象中定义 language 属性,MSHTML 就会尝试使用恰当的脚本引擎。如果选择了错误的脚本引擎,那么通常会产生错误。如果在一个页面中使用了多个 SCRIPT 对象,则可能需要为每个 SCRIPT 对象指定 language 属性,而且推荐这么做。一个文档中 SCRIPT 对象的顺序也很重要,特别是为文档中一个或多个元素指定的脚本事件句柄。扩展标记语言(XML)是 SCRIPT 对象的合法内容,但 XML 并不是一种脚本语言。因此,如果 MSHTML 把包含事件句柄函数的 SCRIPT 对象看作了 XML 数据岛,将会发生错误。之所以会产生这样的结果,是因为 MSHTML 会将第一个有 language 属性的 SCRIPT 对象作为事件句柄的默认引擎块。有关更多信息,请参看示例。

此元素不显示。

此元素需要关闭标签。

Code within the SCRIPT block that is not contained within a function is executed immediately as the page is loaded. To keep scripts from being displayed on down-level browsers, nest the SCRIPT block within a COMMENT block.

Script appearing after a FRAMESET element is ignored.

This element is available in HTML as of Internet Explorer 3.0, and in script as of Internet Explorer 4.0.

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 on a page, 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. Extensible Markup Language (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.

This element is not rendered.

This element requires a closing tag.

示例代码

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;
  alert(islandRoot.nodeName);
}
</SCRIPT>
</head>
<body>
<button onclick="returnIslandRootName()">Test the XML Data Island</button>
</body>
</html>

 

由于 XML 数据岛是定义了 langauge 属性的第一个 SCRIPT 对象,MSHTML 会尝试将 returnIslandRootName 函数以 XML 装入,这当然会导致失败。要修正这个例子,只需要更改 SCRIPT 对象的顺序,如下所示:

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 changed, as shown by the following:

<html>
<head>
<SCRIPT LANGUAGE="Javascript">
function returnIslandRootName()
{
  var islandRoot = document.all["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="returnIslandRootName()">Test the XML Data Island</button>
</body>
</html>