Introduction of HTML <script> Tag
<script> tag of HTML is used to define client side script in web pages. For example, through the <script> tag you can add JavaScript in the HTML document. JavaScript is a client-side scripting language. With the help of this, you can generate dynamic content and validate forms etc.
Attributes of HTML <script> Tag
The attributes that are used in the script tag are given as below.
Src
By using this attribute you can define the URL of the external JavaScript and Its value will be in double quotes.
type
By using this attribute you can define the type of script. If you are using JavaScript, its value will be “text/JavaScript”.
async
The value of this attribute will be in the form of true and false. If you set this attribute true then the script is loaded asynchronously after all the elements of the HTML page are loaded. It is used to reduce page load time.
defer
The value of this attribute will also be in the form of true and false. When you set this attribute true, the script loads after HTML document is parsed.
Syntax of HTML <script> Tag
You can define the script tag like any normal HTML tag. Its simple syntax has been given as below.
<script type = "text/javascript">
// statements
</script>
You use src attribute to include an external JavaScript. Its simple syntax has been given below.
<script type = "text / javascript" src = "external-javascript-URL"> </ script>
As you can see in the syntax which is given above, when you include an external JavaScript in your page, you do not need to define in the script page.
To set the script anonymously, you set the async attribute true. Its simple syntax is being given below.
<script type="text/javascript" src="external-javascript-URL" async="true"> </script>
To load a script after the page is parsed, you set the defer attribute true. Its simple syntax is being given below.
<script type="text/javascript" src ="external-javascript-URL" defer = "true"> </script>
How to use HTML script & noscript Tag?
You can use script tags in two ways in HTML documents.
Embedding Script
In this way, you define script only in the HTML document. By the way, you can define the <script> tag in HTML's <body> tag but most of the time it is defined in the <head> tag.
This is being explained by the example below.
In the above example, the script is embedded in the page itself. This script generates the given output below.
Linking Script
In this way, your script is stored on a server and you link it to your HTML document. For this, you use the src attribute of the <script> tag.
If you want to keep your script hidden and safe, then you can use this method for it. It is also called external javascript. This is being explained by the example below.
In the above example, an external JavaScript is linked to the page. When you define external javascript, save it with a .js extension.
You do not need to define <script> tag when defining an external JavaScript. The above script generates the given output below.
HTML <noscript> Tag
Sometimes the script does not load in the page. It can happen for many reasons. For example, the user is using an old web browser that does not support script or the user has disabled the script itself.
Whatever the reason, in such situations, you can use <noscript> tag which is used to show the user appropriate message when the script is not loaded. You can use this tag anywhere in the head section or in the body section.
An example of this is being given below.
The above script generates the given output below.
0 Comments:
Post a Comment
Thank you for reading this post. Please do not enter any spam link in the comment box.