HTML 5 <script> Tag
Example
Write "Hello world" with JavaScript:
<script type="text/javascript">
document.write("Hello World!")
</script> |
View it » |
Definition and Usage
The <script> tag is used to define a client-side script, such as a
JavaScript.
The script element either contains scripting statements or it points to an
external script file through the src attribute.
The required type attribute specifies the MIME type of the script.
Common uses for JavaScript are image manipulation, form validation, and dynamic
changes of content.
Differences Between HTML 4.01 and HTML 5
The "async" attribute i new in HTML 5.
Some HTML 4.01 attributes are not supported in HTML 5.
Tips and Notes
Note: There are ways a script can be executed:
The async attribute is "true": The script will be executed asynchrously with
the rest of the page, so the script will be executed while the page continues
the parsing.
The async attribute is "false", but the defer attribute is "true": The script
will be executed when the page is finished with the parsing.
Both the async attribute and the defer attribute is "false": The script will
be executed immediately, and the page will wait for the script to finish before
continuing the parsing.
Tip: If there is a src attribute, the <script> element must be empty.
Attributes
New : New in HTML 5.
Attribute |
Value |
Description |
asyncNew |
async |
Defines if the script should be executed asynchronously or
not |
type |
text/ecmascript
text/javascript
application/ecmascript
application/javascript
text/vbscript |
Indicates the MIME type of the script |
charset |
charset |
Defines the character encoding used in script. Not
supported. |
defer |
defer |
Indicates that the script is not going to generate any
document content. The browser can continue parsing and drawing the page |
src |
URL |
Defines a URL to a file that contains the script (instead of inserting the script
into your HTML document, you can refer to a file that contains the script) |
xml:space |
preserve |
Not supported in HTML 5. |
|