English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
HTML <script> tag is used to embed or reference client-side scripts (such as JavaScript). This tag is also commonly referred to as the <script> element. Note: Neither the start tag nor the end tag can be omitted.
Output "HTML5 Script Tag Example":
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>HTML script tag use (oldtoolbag.com)</title> <!-- HTML4 and (x)HTML --> <script type="text}}/javascript" src="functions.js"> <!-- HTML5 --> <script src="/js/functions.js"></script> </head> <body> <script type="text}}/javascript"> document.write("HTML5 Script Tag Example");</script> </body> </html>Test to see ‹/›
In this HTML5In the document example, we used the <script> tag within the <head> tag to reference a JavaScript file named functions.js. We also used the <script> tag within the <body> tag to print the text " HTML5Script tag example.
Please note that,type="text/javascript" HTML5The <script> tag is not needed in the document.IEFirefoxOperaChromeSafari
All mainstream browsers support the <script> tag.
The <script> tag is used to define client-side scripts, such as JavaScript.
The <script> element can contain script statements and can also point to an external script file through the "src" attribute.
JavaScript is usually used for image operations, form validation, and dynamic content changes.
Note:If the "src" attribute is used, the <script> element must be empty.
Note::The start and end tags cannot be omitted.
Tip:See <noscript> element is very useful for users who have disabled scripts in their browsers or whose browsers do not support client-side scripts.
Note: There are several ways to execute external scripts:
If async="async": the script is executed asynchronously relative to the rest of the page (the script will be executed while the page continues to be parsed)
If async is not used and defer="defer": the script will be executed when the page is fully parsed
If neither async nor defer is used: the script is read and executed immediately before the browser continues parsing the page
In HTML 4 In HTML, the "type" attribute is required, but in HTML5 is optional in HTML.
"async" attribute is HTML5 new attribute.
HTML5 No longer supports HTML 4.01 Some properties: "xml:space".
In XHTML, the content type declaration in scripts is #PCDATA (instead of CDATA), which means entities will be parsed.
This means that in XHTML, all special characters should be encoded, or all content should be nested within a CDATA section:
<script type="text}}/javascript"> //<![CDATA[ var i=10; if (i<5) { // Code content } //]]> </script>
New: HTML5 new attribute.
Attribute | Value | Description |
---|---|---|
asyncHTML5 | async | Specifies to execute the script asynchronously (only for external scripts). |
charset | charset | Specifies the character encoding used in the script (only for external scripts). |
defer | defer | Specifies to execute the script after the page has been fully parsed (only for external scripts). |
src | URL | Specifies the URL of the external script. |
type | MIME-type | Specifies the MIME type of the script. |
xml:space | preserve | HTML5 Not supported. Specifies whether to preserve whitespace in the code. |
Supports of <script> tag Global Attributes of HTML.
HTML Tutorial:HTML Script