Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is "<script type='text/javascript'>" incorrect?

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.

  • Where are the MIME types for <script> defined in the html5 W3C specification ?
  • What is browser support like for "text/javascript" and "application/javascript" ?
  • Which should be used ? Alternatively should we just not set 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:

  • script with or without the type
  • JavaScript MIME type
  • Why set MIME type
  • RF4329
  • JavaScript MIME type

The union of the related resources doesn't really answer all three questions.

like image 867
Raynos Avatar asked Jul 24 '11 12:07

Raynos


People also ask

What is script type text JavaScript?

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.

Is script type text/JavaScript necessary?

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.

Can you write scripts in JavaScript?

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.

Does script need tag?

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> .


2 Answers

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.

like image 88
Quentin Avatar answered Sep 22 '22 15:09

Quentin


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.

like image 36
james.garriss Avatar answered Sep 23 '22 15:09

james.garriss