I have seen many different ways in declaring script tag. Some of them are:
Variation 1:
<script type="text/javascript">
//some javascript here
</script>
Variation 2:
<script type="text/javascript">
//<![CDATA[
// some javascript here
//]]>
</script>
Variation 3:
<script language="javascript">
//some javascript here
</script>
Variation 4:
<script>
//some javascript here
</script>
There are also other variations as well. When we work in HTML5 + js then which way is more appropriate in declaring Script tag?
I have seen many different ways in declaring script tag. Some of them are:
Variation 1:
<script type="text/javascript">
//some javascript here
</script>
Variation 2:
<script type="text/javascript">
//<![CDATA[
// some javascript here
//]]>
</script>
Variation 3:
<script language="javascript">
//some javascript here
</script>
Variation 4:
<script>
//some javascript here
</script>
There are also other variations as well. When we work in HTML5 + js then which way is more appropriate in declaring Script tag?
Share Improve this question asked Jan 6, 2014 at 10:07 ktakta 20.2k7 gold badges67 silver badges48 bronze badges 2- Aside from variation 3 (I'm not 100% sure so I'mma leave it out), they're all valid ways. It's basically personal preference at this point as far as I know – Sterling Archer Commented Jan 6, 2014 at 10:09
- do checkout: stackoverflow./questions/66837/… – sv_in Commented Jan 6, 2014 at 10:51
4 Answers
Reset to default 5In HTML5 you don't need an additional attribute like "type". Just declare JavaScript code with <script></script>
.
The default language for script
tags is Javascript, so there is no need to declare it explicitly.
<script>
//some javascript here
</script>
Is the least verbose and most appropriate way to declare a Javascript block. The others are valid, but just redundant.
From MDN:
type: If this attribute is absent, the script is treated as JavaScript
language: Like the type attribute, this attribute identifies the scripting language in use. Unlike the type attribute, however, this attribute’s possible values were never standardized. The type attribute should be used instead.
Variation 3 should not be used as the language
attribute was never standardized and is marked on MDN as deprecated.
As Jack points out, the use of <![CDATA[]]>
is also redundant as it is only used to make sure a document can be parsed as XML.
JavaScript is the default scripting language for HTML5, so you can just use script tag without any type attribute. However, with HTML5, you get some additional features like, async.
Version 2 : format
It skip looking into javascript implementation for W3C validator.
Also it allow to identified by the browser that the JavaScript or text written inside the tag
See this link for More detail:
Javascript and XHTML