If you have already register Login here.
HTML Script Tag
HTML script tag is used to specify client-side script such as JavaScript. It facilitate you to place a script within your HTML document.
JavaScript is used for image manipulation, form validation, and dynamic content.
The <script> element either contains scripting statements, or it points to an external script file through the src attribute.
The syntax of script tag is given below:
<script>
//code to be executed
</script>
Attribute | Description | Compatibility |
---|---|---|
src | It specifies the URL of an external script file. | HTML 4.01, HTML5 |
type | It specifies the media type of the script. | HTML 4.01 |
async | It is a boolean value which specifies that the script is executed asynchronously. | HTML5 |
defer | It is a boolean value which is used to indicate that script is executed after document has been parsed. | HTML 4.01, HTML5 |
charset | charset : Specifies the character encoding used in an external script file | HTML 4.01, HTML5 |
xml:space | preserve : Specifies whether whitespace in code should be preserved | Not supported in HTML5. |
There can be two usage of HTML script tag:
The script tag can be used within <body> or <head> tag to embed the scripting code. Let's see the example to have script tag within HTML body.
<script type="text/javascript">
document.write("JavaScript is a simple language for Easytolearning learners")
</script>
Output
JavaScript is a simple language for Easytolearning learners
The script tag can be used to link external script file by src attribute. It must be used within the <head> tag only.
<script type="text/javascript" src="myjsfile.js" />
Note: If the "src" attribute is present, the <script> element must be empty.
Tip: Also look at the <noscript> element for users that have disabled scripts in their browser, or have a browser that doesn't support client-side scripting.
Note: There are several ways an external script can be executed:
© 2025 Easy To Learning. All Rights Reserved | Design by Easy To Learning