HTML <script> tag
Examples
Through JavaScript output "Hello world":
document.write("Hello World!")
</script>
Browser Support
All major browsers support the <script> tag.
Tag definitions and instructions
<Script> tag is used to define client-side script, such as JavaScript.
<Script> element can contain script statements, you can also "src" attribute points to an external script file.
JavaScript is typically used for image manipulation, form validation, and dynamic content changes.
Tips and Notes
NOTE: If you use the "src" attribute, the <script> element must be empty.
Tip: See the <noscript> element, for those disabled in your browser script or a browser does not support client-side scripting users, this element is very useful.
Note: There are several ways to perform an external script:
- If async = "async": script asynchronously with respect to the rest of the page execution (when the page continues to be parsed, the script will be executed)
- When the script execution will be completed parsing the page: If you do not use the async and defer = "defer"
- If neither use nor async defer: Before the browser to continue parsing the page, immediately read and execute scripts
Differences between HTML 4.01 and HTML5
In HTML 4 in, "type" attribute is required, but is optional in HTML5.
"Async" attribute is new in HTML5 attributes.
HTML5 no longer supported in HTML 4.01 in certain properties: "xml: space".
Differences between HTML and XHTML
In XHTML, the script content type is declared as #PCDATA (instead of CDATA), it means that the entity will be resolved.
This means that in XHTML, all special characters should be encoded or all content nested in a CDATA section:
//<![CDATA[
var i=10;
if (i<5)
{
// some code
}
//]]>
</script>
Attributes
New: HTML5 new property.
属性 | 值 | 描述 |
---|---|---|
async New | async | 规定异步执行脚本(仅适用于外部脚本)。 |
charset | charset | 规定在脚本中使用的字符编码(仅适用于外部脚本)。 |
defer | defer | 规定当页面已完成解析后,执行脚本(仅适用于外部脚本)。 |
src | URL | 规定外部脚本的 URL。 |
type | MIME-type | 规定脚本的 MIME 类型。 |
xml:space | preserve | HTML5 不支持。规定是否保留代码中的空白。 |
Global Properties
<script> tag supports HTML global properties .
related articles
HTML Tutorial: HTML Script