The HTML <script> tag can be placed in the <head> element, as well as within the <body> element. The script execution doesn’t depend on its location in an HTML document, but the scripts, that must be executed first, must be placed in the heading of the document. The <script> tag can be used in an HTML document many times.
The <script> tag comes in pairs. The content is written between the opening (<script>) and closing (</script>) tags.
There are a few ways an external script can be executed:
- The async="async" attribute indicates, that the scripts are executed asynchronously, simultaneously with the loading of the page.
- When there is no async and defer="defer", the script is executed after the loading of the page.
- If there is no async and defer, the script is executed before the loading of the page.
Example of HTML <script> tag:¶
Differences Between HTML 4.01 and HTML5¶
HTML 4 requires the type attribute, whereas it is optional in HTML5. In HTML5, the async attribute is a new one. HTML5 does not support the HTML 4.01 xml:space attribute.
Differences Between HTML and XHTML¶
In XHTML, the content inside scripts is declared as #PCDATA (instead of CDATA). In such cases, the entities will be parsed.
In XHTML, all special characters must be encoded, or all the content must be wrapped inside a CDATA section.
|async||async||Defines that the script is executed asynchronously. (For external scripts only). |
Not supported in IE9 and older versions.
|defer||defer||Defines, that the script must be executed after the loading of the page. (For external scripts only).|
|type||media_type||Defines the MIME-type of the script.|
| || || || || |