The type attribute in JavaScript is optional since the introduction of HTML5 brought some new improvements. JavaScript became the default language for HTML5 and modern browsers. So, now adding text/javascript isn't required in <script> tag.
The type attribute is required in HTML4. 01 but optional in HTML5. The type shows what type to be parsed. NOTE: if the type attribute is a audio, HTML, video, or any MIME type besides JavaScript the code will NOT be executed.
We can use a <script> tag to add JavaScript code to a page. The type and language attributes are not required. A script in an external file can be inserted with <script src="path/to/script. js"></script> .
See Crockford's write-up on the <script>
tag, most notably:
Do not use the
<!-- //-->
hack with scripts. It was intended to prevent scripts from showing up as text on the first generation browsers Netscape 1 and Mosaic. It has not been necessary for many years.<!-- //-->
is supposed to signal an HTML comment. Comments should be ignored, not compiled and executed. Also, HTML comments are not to include--
, so a script that decrements has an HTML error.
...
type="text/javascript"
This attribute is optional. Since Netscape 2, the default programming language in all browsers has been JavaScript. In XHTML, this attribute is required and unnecessary. In HTML, it is better to leave it out. The browser knows what to do.
It's a Crockford recommendation. I know I've seen it echoed elsewhere (ppk maybe?). The HTML5 spec does not require it.
Oddly, it's become somewhat au courant to use the "type" attribute to mark <script>
blocks that you don't want to be evaluated:
<script type='text/html-template'>
<div> this is a template </div>
</script>
By giving a weird non-JavaScript type, you get a way to stuff raw text into the page for use by other JavaScript code (which is presumably in script block that can be evaluated).
HTML5 doesn't need the type="text/javascript"
(it's the default).
CDATA
is only neeed for XHTML pages, if the script has any HTML characters (like '<' and '>') in it.
<!--
should only be needed for OLD browsers.
Well, I am tempted to say that nobody is using text/javascript
any more, and that even minification tools would probably remove it...
Indeed, Facebook SDK documentation specifies just <script>
.
However,
Google SDK documentation still has text/javascript
.
Amazon SDK documentation still has text/javascript
.
Linkedin API documentation still has text/javascript
.
Instagram is still using text/javascript
.
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