For adding JavaScript to HTML, I have seen people use
<script language=javascript>
and
<script type="text/javascript">
It doesn’t seem like whether the script is embedded or external influences this decision.
Which one is preferred and why?
The HTML <script> type Attribute is used to specify the MIME type of script and identify the content of the Tag. It has a Default value which is “text/javascript”. Syntax: <script type="media_type"> Attribute Values: It contains a single value i.e media_type which specifies the MIME type of script.
No, it is not necessary if you use HTML5. "Differences Between HTML 4.01 and HTML5 In HTML5, the type attribute is no longer required for JavaScript. The default value is "application/javascript"." Source: https://www.w3schools.com/tags/att_script_type.asp To use HTML5 write <!
In a nutshell, JavaScript is a dynamic scripting language supporting prototype based object construction. The basic syntax is intentionally similar to both Java and C++ to reduce the number of new concepts required to learn the language.
<script language="javascript">
was used in very old browsers, and is deprecated.
<script type="text/javascript">
is the HTML 4 standard.
In HTML 5, the type
parameter is optional (text/javascript
is the default), so you can just do <script>
.
As a neat hack, if you put an invalid type
, the script won't be ran, but you can still read the data in JavaScript. Some template libraries do this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With