admin管理员组

文章数量:1134682

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?

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?

Share Improve this question edited Aug 17, 2019 at 11:51 Sebastian Simon 19.5k8 gold badges60 silver badges84 bronze badges asked Nov 4, 2010 at 21:06 Mark BaijensMark Baijens 13.2k11 gold badges50 silver badges76 bronze badges 4
  • possible dupe/explanation: stackoverflow.com/questions/876561/… – Benn Commented Nov 4, 2010 at 21:10
  • See also stackoverflow.com/questions/2325571/… – Gumbo Commented 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 Commented Jan 13, 2014 at 18:47
  • Possible duplicate of When serving JavaScript files, is it better to use the application/javascript or application/x-javascript – Chuck Le Butt Commented Apr 23, 2018 at 14:15
Add a comment  | 

5 Answers 5

Reset to default 251

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.

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.

application because .js-Files aren't something a user wants to read but something that should get executed.

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.

本文标签: When to use the JavaScript MIME type applicationjavascript instead of textjavascriptStack Overflow