Based on the question jQuery code not working in IE, text/javascript
is used in HTML documents so Internet Explorer can understand it.
But I’m wondering, when would you use application/javascript
, and more importantly, why would you use it 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.
The problem with Javascript's MIME type is that there hasn't been a standard for years. Now we've got application/javascript as an official MIME type.
But actually, the MIME type doesn't matter at all, as the browser can determine the type itself. That's why the HTML5 specs state that the type="text/javascript"
is no longer required.
application
because .js
-Files aren't something a user wants to read but something that should get executed.
text
, and binary
as either application
-OR the "purpose" of said type as in "image", or "document", etc.
application/javascript is the correct type to use but since it's not supported by IE6-8 you're going to be stuck with text/javascript. If you don't care about validity (HTML5 excluded) then just don't specify a type.
application/javascript
.
There's been a lot of confusion and disagreement about this in the past, which other answers explain in some detail.
RFC9239 finally resolves this confusion by aligning with implementation reality. application/javascript
is now officially obsolete; text/javascript
is the only correct JavaScript MIME type.
Success story sharing
application/javascript
and IE running on compatibility mode withIE=8
. Seems as if inline scripts are not properly evaluated.text/javascript
works fine there.X-Content-Type-Options: nosniff
to prevent the browser from interpreting the type.Server: nginx
or whatever nginx sends. As if whoever is capable of finding a hole needs explicit header to know what server you run...