When to use the JavaScript MIME type application/javascript instead of text/javascript?
In theory, according to RFC 4329, application/javascript
.
The reason it is supposed to be application
is not anything to do with whether the type is readable or executable. It's because there are custom charset-determination mechanisms laid down by the language/type itself, rather than just the generic charset
parameter. A subtype of text
should be capable of being transcoded by a proxy to another charset, changing the charset parameter. This is not true of JavaScript because:
a. the RFC says user-agents should be doing BOM-sniffing on the script to determine type (I'm not sure if any browsers actually do this though);
b. browsers use other information—the including page's encoding and in some browsers the script charset
attribute—to determine the charset. So any proxy that tried to transcode the resource would break its users. (Of course in reality no-one ever uses transcoding proxies anyway, but that was the intent.)
Therefore the exact bytes of the file must be preserved exactly, which makes it a binary application
type and not technically character-based text
.
For the same reason, application/xml
is officially preferred over text/xml
: XML has its own in-band charset signalling mechanisms. And everyone ignores application
for XML, too.
text/javascript
and text/xml
may not be the official Right Thing, but there are what everyone uses today for compatibility reasons, and the reasons why they're not the right thing are practically speaking completely unimportant.
application/javascript or text/javascript
The MDN article links to the WHATWG (Web Hypertext Application Technology Working Group, which consists of people from Apple, Google, Mozilla, Microsoft and others) HTML Living Standard that was last updated a few days ago. This is the current most up to date HTML specification and it says text/javascript
.
Servers should use text/javascript for JavaScript resources. Servers should not use other JavaScript MIME types for JavaScript resources, and must not use non-JavaScript MIME types.
In reality though no browser will break on either text/javascript
or application/javascript
so it doesn't really matter. For new projects I would definitely use text/javascript
but I won't go back and update my old ones.
text/javascript vs application/javascript
Per IETF RFC 9239 text/javascript
is now standard and application/javascript
is now considered obsolete.
The media type registrations herein are divided into two major categories: (1) the sole media type
"text/javascript", which is now in common usage and (2) all of the media types that are obsolete
(i.e., "application/ecmascript", "application/javascript", "application/x-ecmascript", "application/
x-javascript", "text/ecmascript", "text/javascript1.0", "text/javascript1.1", "text/javascript1.2", "text/
javascript1.3", "text/javascript1.4", "text/javascript1.5", "text/jscript", "text/livescript", and "text/xecmascript").
See further notes in When serving JavaScript files, is it better to use the application/javascript or application/x-javascript
Why write script type=text/javascript when the mime type is set by the server?
Douglas Crockford says:
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.
He also says:
W3C did not adopt the
language
attribute, favoring instead atype
attribute which takes a MIME type.
Unfortunately, the MIME type was not
standardized, so it is sometimes
"text/javascript"
or
"application/ecmascript"
or something
else. Fortunately, all browsers will
always choose JavaScript as the
default programming language, so it is
always best to simply write<script>
.
It is smallest, and it works on the
most browsers.
For entertainment purposes only, I tried out the following five scripts
<script type="application/ecmascript">alert("1");</script>
<script type="text/javascript">alert("2");</script>
<script type="baloney">alert("3");</script>
<script type="">alert("4");</script>
<script >alert("5");</script>
On Chrome, all but script 3 (type="baloney"
) worked. IE8 did not run script 1 (type="application/ecmascript"
) or script 3. Based on my non-extensive sample of two browsers, it looks like you can safely ignore the type
attribute, but that it you use it you better use a legal (browser dependent) value.
When serving JavaScript files, is it better to use the application/javascript or application/x-javascript
According to the IETF's ECMAScript Media Types Updates as of 22 February 2021, the RFC-4329 is obsolete.
Therefore:
text/javascript
is a recommended standard (both by IETF and by MDN)application/x-javascript
was experimental while deciding to move to…application/javascript
is obsolete
Difference between application/x-javascript and text/javascript content types
text/javascript
is obsolete, and application/x-javascript
was experimental (hence the x-
prefix) for a transitional period until application/javascript
could be standardised.
You should use application/javascript
. This is documented in the RFC.
As far a browsers are concerned, there is no difference (at least in HTTP headers). This was just a change so that the text/*
and application/*
MIME type groups had a consistent meaning where possible. (text/*
MIME types are intended for human readable content, JavaScript is not designed to directly convey meaning to humans).
Note that using application/javascript
in the type
attribute of a script element will cause the script to be ignored (as being in an unknown language) in some older browsers. Either continue to use text/javascript
there or omit the attribute entirely (which is permitted in HTML 5).
This isn't a problem in HTTP headers as browsers universally (as far as I'm aware) either ignore the HTTP content-type of scripts entirely, or are modern enough to recognise application/javascript
.
What is the javascript MIME type for the type attribute of a script tag?
This is a common mistake. The MIME type for javascript wasn't standardized for years. It's now officially: "application/javascript".
The real kicker here is that most browsers won't use that attribute anyway, at least not in the case of the script tag. They actually peek inside the packet and determine the type for themselves.
So the bottom line is that the type="text/javascript" doesn't do anything as far as the javascript is concerned, but it's part of the spec for both HTML 4 and XHTML 1.0.
Related Topics
How to Search JSON Tree with Jquery
Check If Jquery Has Been Loaded, Then Load It If False
How to Stop Babel from Transpiling 'This' to 'Undefined' (And Inserting "Use Strict")
Array.Length Is Zero, But the Array Has Elements in It
Typescript Type 'String' Is Not Assignable to Type
What's the Best Way to Loop Through a Set of Elements in JavaScript
JavaScript Accessing Inner Dom of Svg
How to Check If a Number Is Between Two Values
How to Trigger a Link's (Or Any Element'S) Click Event Through JavaScript
Validate That a String Is a Positive Integer
Getting Binary Content in Node.Js Using Request
JavaScript Audio Play on Click
JavaScript Settimeout() Won't Wait to Execute
Change Image Size with JavaScript
Split String Only on First Instance of Specified Character
How to Trigger Ngclick Programmatically