How to Use of HTML Script Type Attribute

Mehvish Ashiq Feb 02, 2024
  1. Use of the HTML Script type Attribute in HTML5
  2. Use of the HTML Script type Attribute in HTML 4.01, XHTML 1.0, and VBScript
How to Use of HTML Script Type Attribute

This tutorial teaches about using the HTML script type attribute. You can also call it JavaScript MIME (Multipurpose Internet Mail Extensions) type.

Before that, it is necessary to know the reason for using the type attribute. It indicates the document’s format and nature.

We can also say it represents the script’s type, application/javascript (default value), or application/ecmascript. You can check this for more media types.

We understand the script type attribute and why to use it, but where? We don’t need to use the type attribute within the <script> tag while we use modern browsers because they have it by default.

Also, if you are using HTML5 then you can simply write <script> ... </script> because it has type attribute optional and defaulting text/javascript.

In another case, you have to specify the type attribute while using HTML 4.01 and XHTML 1.0 because both require it.

You also have to use the type attribute if the content is not interpreted as JavaScript, for instance, VBScripts.

You can practice each possible scenario by taking advantage of the following startup code.

Use of the HTML Script type Attribute in HTML5

There is no need to use the type attribute within the <script> tag for HTML5. See the following example code.

<!DOCTYPE html>
<html>
	<head>
 		<title>JavaScript MIME Type</title>
 		<script>
 			console.log("Hello JavaScript MIME Type");
 		</script>
 	</head>
 <body>
 	<h1>Let's learn the use of JavaScript MIME type.</h1>
 </body>
</html>

Use of the HTML Script type Attribute in HTML 4.01, XHTML 1.0, and VBScript

Using modern browsers, you may use the <script> tag without the type attribute while writing HTML 4.01 and XHTML 1.0. But it is good to use the type attribute because both require.

It would help if you also used the type attribute while using VBScripts. Check out the following code snippets.

HTML 4.01 Code:

<!"-//W3C//DTD HTML 4.01//EN"
 "https://www.w3.org/TR/html4/strict.dtd">

<html>
 	<head>
 		<title>JavaScript MIME Type</title>
 		<script type="text/javascript">
 			console.log("Hello JavaScript MIME Type");
 		</script>
 	</head>
 <body>
 	<h1>Let's learn the use of JavaScript MIME type.</h1>
 </body>
</html>

XHTML 1.0 Code:

<! "-//W3C//DTD XHTML 1.0 Strict//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
 	<head>
 		<title>JavaScript MIME Type</title>
 		<script type="text/javascript">
 			console.log("Hello JavaScript MIME Type");
 		</script>
 	</head>
 <body>
 	<h1>Let's learn the use of JavaScript MIME type.</h1>
 </body>
</html>

HTML 4.01 with VBScript:

<! "-//W3C//DTD HTML 4.01//EN"
 "https://www.w3.org/TR/html4/strict.dtd">

<html>
 	<head>
 		<title>A document with SCRIPT</title>
 		<script type="text/vbscript" src="http://someplace.com/progs/vbcalc">
 		</script>
 	</head>
 <body>
 	<h1>Let's learn the use of JavaScript MIME type.</h1>
 	<script type="text/javascript">
 		console.log("HELLO VBScript")
 	</script>
 </body>
</html>
Mehvish Ashiq avatar Mehvish Ashiq avatar

Mehvish Ashiq is a former Java Programmer and a Data Science enthusiast who leverages her expertise to help others to learn and grow by creating interesting, useful, and reader-friendly content in Computer Programming, Data Science, and Technology.

LinkedIn GitHub Facebook

Related Article - JavaScript Attribute