问题描述
根据问题 jQuery代码在IE中不起作用,HTML文档中使用了text/javascript
,因此Internet Explorer可以理解它
Based on the question jQuery code not working in IE, text/javascript
is used in HTML documents so Internet Explorer can understand it.
但是我想知道,您什么时候使用application/javascript
,更重要的是,为什么要使用它代替text/javascript
?
But I’m wondering, when would you use application/javascript
, and more importantly, why would you use it instead of text/javascript
?
推荐答案
理论上,根据 RFC 4329 ,application/javascript
.
应该为application
的原因与该类型是可读还是可执行无关.这是因为语言/类型本身设置了自定义字符集确定机制,而不仅仅是通用的charset
参数. text
的子类型应该能够被代理更改为另一个字符集,从而更改字符集参数. 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. RFC表示用户代理应该对脚本进行BOM嗅探以确定类型(不过,我不确定是否有任何浏览器确实这样做);
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.浏览器使用其他信息(包括页面的编码以及 some 浏览器中的script charset
属性)来确定字符集.因此,任何尝试对资源进行转码的代理都将破坏其用户. (当然,实际上,没有人曾经使用过转码代理,但这只是目的.)
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.)
因此,必须精确地保留文件的确切字节 ,这使其成为二进制application
类型,而不是从技术上基于字符的text
.
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/xml
在官方上优于text/xml
:XML具有自己的带内字符集信令机制.每个人也都忽略了XML的application
.
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
和text/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.
这篇关于什么时候使用JavaScript MIME类型application/javascript而不是text/javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!