On aminutewithbrendan, brendan eich makes an off hand comment implying that serving scripts as
<script type='text/javascript'></script>
is not correct because "text/javascript"
is not a valid MIME type and he states "application/javascript"
is a valid MIME type.
I only care about serving HTML5 as the doctype.
<script>
defined in the html5 W3C specification ?"text/javascript"
and "application/javascript"
?type
at all?Literal Quote from brendan: (1:48)
... or script type equals application/javascript or application/ecmascript, those are the official MIME types or either one of those made-up ones from HTML4 like text/javascript ...
Related:
The union of the related resources doesn't really answer all three questions.
The <script> tag is used to embed a client-side script (JavaScript). The <script> element either contains scripting statements, or it points to an external script file through the src attribute. Common uses for JavaScript are image manipulation, form validation, and dynamic changes of content.
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.
To write a JavaScript, you need a web browser and either a text editor or an HTML editor. Once you have the software in place, you can begin writing JavaScript code. To add JavaScript code to an HTML file, create or open an HTML file with your text/HTML editor.
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> .
Where are the MIME types for
<script>
defined in the html5 W3C specification ?
Nowhere, it has a list (which includes some experimental and deprecated ones) but states that you can use any MIME type you like. MIME types are defined by IANA and text/javascript
is officially marked as obsolete in favour of application/javascript
What is browser support like for "text/javascript" and "application/javascript" ?
Not good enough. There are still plenty of browsers around that don't recognise the latter. (This is, however, only a problem with the type attribute, you can set the HTTP Content-Type header correctly without worrying).
Which should be used ? Alternatively should we just not set type at all?
Since you only care about HTML 5, just omit the type attribute entirely. It is optional and the default language is JavaScript.
I think Brandon is (at least) partially wrong. The latest editor's draft of HTML5 (15 Aug 2011) says:
The type attribute gives the language of the script or format of the data. If the attribute is present, its value must be a valid MIME type. The charset parameter must not be specified. The default, which is used if the attribute is absent, is "text/javascript".
So if you do not include a type
attribute, the default value is "text/javascript". If that is the default value, it must be a valid MIME type.
What are the other valid MIME types? The spec doesn't seem to give an example list, but it does specify the required format when it says:
A string is a valid MIME type if it matches the media-type rule defined in section 3.7 "Media Types" of RFC 2616
Which you can have the joy of reading here:
http://www.ietf.org/rfc/rfc2616.txt
Edit: Quentin is right: For HTML5, there's no need to include a type
attribute, assuming you're using 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