何时使用 JavaScript MIME 类型 application/javascript 而不是 text/javascript?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4101394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When to use the JavaScript MIME type application/javascript instead of text/javascript?
提问by Mark Baijens
Based on the question jQuery code not working in IE, text/javascript
is used in HTML documents so Internet Explorer can understand it.
基于jQuery code not working in IE 的问题,text/javascript
用于 HTML 文档以便 Internet Explorer 可以理解它。
But I'm wondering, when would you use application/javascript
, and more importantly, why would you use it instead of text/javascript
?
但我想知道,您什么时候会使用application/javascript
,更重要的是,为什么要使用它而不是text/javascript
?
回答by bobince
In theory, according to RFC 4329, application/javascript
.
从理论上讲,根据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:
它应该application
是的原因与类型是否可读或可执行无关。这是因为语言/类型本身规定了自定义字符集确定机制,而不仅仅是通用charset
参数。的子类型text
应该能够被代理转码为另一个字符集,更改字符集参数。这不适用于 JavaScript,因为:
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);
一种。RFC 说用户代理应该对脚本进行 BOM 嗅探以确定类型(我不确定是否有任何浏览器确实这样做了);
b. browsers use other information—the including page's encoding and in somebrowsers 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.)
湾 浏览器使用其他信息——包括页面的编码和在某些浏览器中的script charset
属性——来确定字符集。因此,任何试图对资源进行转码的代理都会破坏其用户。(当然,实际上无论如何都没有人使用转码代理,但这就是意图。)
Therefore the exact bytes of the file must be preserved exactly, which makes it a binary application
type and not technically character-based text
.
因此,必须准确地保留文件的确切字节,这使其成为二进制application
类型,而不是技术上基于字符的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.
出于同样的原因,application/xml
官方更喜欢text/xml
:XML 有自己的带内字符集信号机制。每个人也忽略application
了 XML。
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.
text/javascript
并且text/xml
可能不是官方正确的东西,但是出于兼容性原因,今天每个人都使用什么,而为什么它们不是正确的东西的原因实际上完全不重要。
回答by Harmen
The problem with Javascript's MIME type is that there hasn't been a standard for years. Now we've got application/javascriptas an official MIME type.
Javascript 的 MIME 类型的问题是多年来一直没有标准。现在我们已经将application/javascript作为官方 MIME 类型。
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.
但实际上,MIME 类型根本无关紧要,因为浏览器可以自行确定类型。这就是 HTML5 规范声明type="text/javascript"
不再需要 的原因。
回答by thejh
application
because .js
-Files aren't something a user wants to read but something that should get executed.
application
因为.js
-Files 不是用户想要阅读的东西,而是应该执行的东西。
回答by Radu
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 是可以使用的正确类型,但由于 IE6-8 不支持它,因此您将被 text/javascript 困住。如果您不关心有效性(HTML5 除外),则不要指定类型。