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?
-
possible dupe/explanation: stackoverflow.com/questions/876561/…Benn– Benn2010年11月04日 21:10:02 +00:00Commented Nov 4, 2010 at 21:10
-
See also stackoverflow.com/questions/2325571/…Gumbo– Gumbo2010年11月04日 21:38:14 +00:00Commented Nov 4, 2010 at 21:38
-
possible duplicate of What is the Javascript MIME Type? What belongs in the type attribute of a script tag?Bergi– Bergi2014年01月13日 18:47:08 +00:00Commented Jan 13, 2014 at 18:47
-
Possible duplicate of When serving JavaScript files, is it better to use the application/javascript or application/x-javascriptChuck Le Butt– Chuck Le Butt2018年04月23日 14:15:31 +00:00Commented Apr 23, 2018 at 14:15
5 Answers 5
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.
6 Comments
application/javascript and IE running on compatibility mode with IE=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...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.
Comments
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.
1 Comment
application because .js-Files aren't something a user wants to read but something that should get executed.
5 Comments
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.
5 Comments
application/javascript.