text/javascript 和 application/javascript 有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6122905/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:20:35  来源:igfitidea点击:

what is difference between text/javascript and application/javascript

javascript

提问by Avinash

Possible Duplicate:
When serving JavaScript files, is it better to use the application/javascript or application/x-javascript

可能的重复:
在提供 JavaScript 文件时,使用 application/javascript 还是 application/x-javascript 更好

what is difference between text/javascript and application/javascript?

text/javascript 和 application/javascript 有什么区别?

As IE will completely ignore the script tag if you have set type attribute to application/javascript.

如果您将 type 属性设置为 application/javascript,IE 将完全忽略脚本标记。

But what is the main difference and in which case we need to use the particular.

但是主要区别是什么,在这种情况下我们需要使用特定的。

回答by Quentin

text/javascriptwas introduced when the web was young and people hadn't thought things through.

text/javascript当网络还很年轻并且人们还没有考虑清楚时,它就被引入了。

Then people thought things through, and decided that text/*should be reserved for things designed to be human readable (which is why some XML is text/xmland other XML is application/xml). JavaScript is not human readable, so text/javascriptwas deprecated and application/javascriptwas introduced to replace it.

然后人们仔细考虑了事情,并决定text/*应该保留给设计为人类可读的东西(这就是为什么有些 XML 是text/xml而其他 XML 是application/xml)。JavaScript 不是人类可读的,因此text/javascript被弃用application/javascript并被引入以取代它。

Years later, some browsers still haven't caught up.

多年后,一些浏览器仍然没有赶上。

You can configure your server to always serve application/javascriptin the HTTP headers; browsers that don't support it also pay no attention to the actual content-type.

您可以将服务器配置为始终application/javascript在 HTTP 标头中提供服务;不支持它的浏览器也不关心实际的内容类型。

For the time being, if you are writing HTML 4 or XHTML 1, specify text/javascriptin the type attribute for the sake of backwards compatibility. If you are writing HTML 5, then omit the type attribute (as it is now optional).

目前,如果您正在编写 HTML 4 或 XHTML 1,text/javascript为了向后兼容,请在 type 属性中指定。如果您正在编写 HTML 5,则省略 type 属性(因为它现在是可选的)。

回答by jsalonen

HTML 4.01 (1999) specification suggests using MIME type text/javascript(http://www.w3.org/TR/1999/REC-html401-19991224/interact/scripts.html#h-18.2.2.2).

HTML 4.01 (1999) 规范建议使用 MIME 类型text/javascript( http://www.w3.org/TR/1999/REC-html401-19991224/interact/scripts.html#h-18.2.2.2)。

However, RFC 4329 (2006) now recommends the use of application/javascript.

但是,RFC 4329 (2006) 现在建议使用application/javascript.

It seems that historically text/javascriptwas used a lot and since it was the type that browsers most likely supported, this was the type that got suggested also in the HTML specification. Ideally, you would use application/javascript.

似乎历史text/javascript上使用了很多,并且由于它是浏览器最有可能支持的类型,因此这也是 HTML 规范中建议的类型。理想情况下,您将使用application/javascript.

In practice you may need to use text/javascriptto provide compatibility with less-conforming browsers.

在实践中,您可能需要使用text/javascript来提供与不太符合要求的浏览器的兼容性。