您需要在 <script> 标签中指定 text/javascript 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5265202/
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
Do you need text/javascript specified in your <script> tags?
提问by cmcculloh
I read somewhere that you no longer need things like type="text/javascript"
and the weird CDATA
and <!--
things in your script tags. So, instead of:
我读的地方,你不再需要的东西像type="text/javascript"
和怪异CDATA
,并<!--
在脚本标记的东西。所以,而不是:
<script type="text/javascript">
//<![CDATA[
<!--
//your script here
-->
//]]>
</script>
You would just do:
你会这样做:
<script>
//your script here
</script>
I can't remember where I read this though. It was from a Google or Yahoo engineer I think, and they specifically mentioned which browsers required these archaic constructs and why. Anyone know what blog post/article this was talked about, or have a good resource talking about this?
我不记得我在哪里读到这个。我认为它来自 Google 或 Yahoo 工程师,他们特别提到了哪些浏览器需要这些过时的结构以及原因。任何人都知道这是什么博客文章/文章,或者有一个很好的资源来谈论这个?
采纳答案by bdukes
See Crockford's write-up on the <script>
tag, most notably:
参见Crockford 在<script>
标签上的文章,最值得注意的是:
Do not use the
<!-- //-->
hack with scripts. It was intended to prevent scripts from showing up as text on the first generation browsers Netscape 1 and Mosaic. It has not been necessary for many years.<!-- //-->
is supposed to signal an HTML comment. Comments should be ignored, not compiled and executed. Also, HTML comments are not to include--
, so a script that decrements has an HTML error.
不要在
<!-- //-->
脚本中使用hack。它旨在防止脚本在第一代浏览器 Netscape 1 和 Mosaic 上显示为文本。多年来一直没有必要。<!-- //-->
应该表示 HTML 注释。注释应该被忽略,而不是编译和执行。此外,HTML 注释不包括--
,因此递减的脚本会出现 HTML 错误。
...
...
type="text/javascript"
This attribute is optional. Since Netscape 2, the default programming language in all browsers has been JavaScript. In XHTML, this attribute is required and unnecessary. In HTML, it is better to leave it out. The browser knows what to do.
type="text/javascript"
该属性是可选的。从 Netscape 2 开始,所有浏览器的默认编程语言都是 JavaScript。在 XHTML 中,此属性是必需的,也是不必要的。在 HTML 中,最好将其排除在外。浏览器知道该做什么。
回答by Pointy
It's a Crockfordrecommendation. I know I've seen it echoed elsewhere (ppk maybe?). The HTML5 spec does not require it.
这是克罗克福德的建议。我知道我在其他地方看到过它的呼应(也许是 ppk?)。HTML5 规范不需要它。
Oddly, it's become somewhat au courantto use the "type" attribute to mark <script>
blocks that you don'twant to be evaluated:
奇怪的是,它已经成为有些AU柯朗使用“类型”属性来标记<script>
你块不希望进行评估:
<script type='text/html-template'>
<div> this is a template </div>
</script>
By giving a weird non-JavaScript type, you get a way to stuff raw text into the page for use by other JavaScript code (which is presumably in script block that canbe evaluated).
通过提供一个奇怪的非 JavaScript 类型,您可以获得一种将原始文本填充到页面中以供其他 JavaScript 代码使用的方法(这可能在可以评估的脚本块中)。
回答by Rocket Hazmat
HTML5 doesn't need the type="text/javascript"
(it's the default).
HTML5 不需要type="text/javascript"
(这是默认设置)。
CDATA
is only neeed for XHTML pages, if the script has any HTML characters (like '<' and '>') in it.
CDATA
如果脚本中包含任何 HTML 字符(如“<”和“>”),则只需要 XHTML 页面。
<!--
should only be needed for OLD browsers.
<!--
只有旧浏览器才需要。
回答by osa
Well, I am tempted to say that nobody is using text/javascript
any more, and that even minification tools would probably remove it...
Indeed, Facebook SDK documentationspecifies just <script>
.
好吧,我很想说没有人再使用text/javascript
它了,即使是缩小工具也可能会删除它……事实上,Facebook SDK 文档只指定了<script>
.
However,
Google SDK documentationstill has text/javascript
.
但是,
Google SDK 文档仍然有text/javascript
.
Amazon SDK documentationstill has text/javascript
.
Amazon SDK 文档仍然有text/javascript
.
Linkedin API documentationstill has text/javascript
.
Linkedin API 文档仍然有text/javascript
.
Instagramis still using text/javascript
.
Instagram仍在使用text/javascript
.
回答by Mithun Sreedharan
The typeattribute identifies the scripting language of code embedded within a script element or referenced via the element's src attribute. This is specified as a MIME type; examples of supported MIME types include text/javascript, text/ecmascript, application/javascript, and application/ecmascript.
该类型属性标识嵌入一个脚本元素内或通过该元素的src属性所引用的代码中的脚本语言。这被指定为 MIME 类型;支持的 MIME 类型示例包括 text/javascript、text/ecmascript、application/javascript 和 application/ecmascript。
According to HTML 4.01 Specification
The typeattribute specifies the scripting language of the element's contents and overrides the default scripting language. The scripting language is specified as a content type (e.g., "text/javascript"). Authors must supply a value for this attribute. There is no default value for this attribute.
该类型属性指定的元素内容的脚本语言和覆盖默认脚本语言。脚本语言被指定为内容类型(例如,“text/javascript”)。作者必须为此属性提供一个值。此属性没有默认值。
But in HTML5text/javascript
is the default type, so you can omit
但是在HTML5 中text/javascript
是默认类型,所以可以省略
The typeattribute gives the language of the script or format of the data. If the attribute is present, its value must be a valid MIME type. The charset parameter must not be specified. The default, which is used if the attribute is absent, is "text/javascript".
该类型属性给出了数据的脚本或格式的语言。如果该属性存在,则其值必须是有效的 MIME 类型。不得指定字符集参数。如果属性不存在,则使用的默认值是“text/javascript”。
回答by FatherStorm
you may be thinking of this article herewith the dependency being that scripts default to text/javascript in HTML5 automatically, while non-HTML5 browsers still expect that you define the type specifically spec-wise even though they will almost always guess text/javascript anyways.
您可能会在这里考虑这篇文章,其中依赖项是 HTML5 中的脚本默认为 text/javascript,而非 HTML5 浏览器仍然希望您根据规范来具体定义类型,尽管他们几乎总是会猜测 text/javascript .
回答by hellatan
it's up to the browser to interpret the script
block correctly based on the headers, i believe, and not the type
attribute. So to answer your question, no it is not required for modern browsers (i'm talking IE7+, FF, Webkit). If you are supporting older browsers than that...I feel sorry for you =)
script
我相信,这取决于浏览器根据标题正确解释块,而不是type
属性。所以要回答你的问题,现代浏览器不需要它(我说的是 IE7+、FF、Webkit)。如果您支持比这更旧的浏览器...我为您感到抱歉 =)
回答by brennanyoung
If you're putting a script tag inside SVGyou mustspecify the type attribute. And it should be "text/ecmascript"
rather than "text/javascript"
.
如果您在 SVG 中放置脚本标记,则必须指定 type 属性。它应该是"text/ecmascript"
而不是"text/javascript"
。
If your script is inline (not linked) you will need to wrap the script body in a CDATA declaration too. The inline script boilerplate for SVG(and other XML variants) is thus
如果您的脚本是内联的(未链接),您还需要将脚本主体包装在 CDATA 声明中。因此,SVG(和其他 XML 变体)的内联脚本样板是
<script type="text/ecmascript">
<![CDATA[
// your javascript code goes here
]]>
</script>
These might be special cases 'in the wild', but they are real enough, and SVG use is growing, so it's incorrect for anyone else to suggest that the type attribute and CDATA are entirely obsolete in modern browsers. The use cases are narrow, yes, but not unheard of.
这些可能是“野外”的特殊情况,但它们已经足够真实,并且 SVG 的使用正在增长,因此其他人认为 type 属性和 CDATA 在现代浏览器中完全过时是不正确的。用例很窄,是的,但并非闻所未闻。
"Change the environment to its opposite and every piece of wisdom becomes the worst of folly." - Ashby
“把环境改变成它的对立面,每一条智慧都会变成最糟糕的愚蠢。” - 阿什比
回答by Andy Young
The HTML5 specification urges authors to omit the attribute rather than provide a redundant MIME type.MDN
HTML5 规范敦促作者省略该属性而不是提供冗余的 MIME 类型。MDN
The MIME Sniffing Standardlets JavaScript be served using any MIME type (Multipurpose Internet Mail Extensions) that matches the following:
该MIME探查标准允许使用JavaScript任何MIME类型(送达多用途Internet邮件扩展)符合以下几点:
<script type="application/javascript"></script>
<script type="application/ecmascript"></script>
<script type="text/javascript"></script>
<script type="text/ecmascript"></script>
回答by Michael
Well i keep seeing more examples without the text/javascript but for some reason my scripts wont work in FF when i do so. I would recommend keeping the text/javascript declaration. The CDATA tag prevents javascript from being shown as plain text in your website if your browser has javascript turned off. Personally i don't use those tags anymore don't think there's allot of users out there without and if they are out there they might wanna grow some brains :P
好吧,我一直在看到更多没有文本/javascript 的示例,但是由于某种原因,当我这样做时,我的脚本将无法在 FF 中工作。我建议保留 text/javascript 声明。如果您的浏览器关闭了 javascript,CDATA 标签会阻止 javascript 在您的网站中显示为纯文本。就我个人而言,我不再使用这些标签了,不要认为外面有很多用户,如果他们在外面,他们可能想要培养一些大脑:P