省略 type="text/javascript" language="javascript"
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2626563/
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
Leaving out type="text/javascript" language="javascript"
提问by PositiveGuy
Most of the script tags I create, I always include type="text/javascript" language="javascript"in the tag. My boss however does not. Sometimes he excludes both, sometimes just has language=javascripteven without the quotes
我创建的大多数脚本标签,我总是包含type="text/javascript" language="javascript"在标签中。但是我的老板没有。有时他排除两者,有时language=javascript甚至没有引号
Now we have not had an issue in any of the major browsers with his tags. I'm talking about all versions of IE, FF, Safari, and Chrome.
现在我们在他的标签的任何主要浏览器中都没有问题。我指的是所有版本的 IE、FF、Safari 和 Chrome。
Personally I feel it's laziness and just totally improper and bad coding practice to leave stuff out like this even if it works without it.
就我个人而言,即使没有它也能正常工作,我个人觉得像这样忽略这样的东西是很懒惰的,而且是完全不正确和糟糕的编码实践。
Anyone know if both should be included or just one or is it ok to leave both out in ASP.NET?
任何人都知道是否应该包含两者或只包含一个,或者可以在 ASP.NET 中将两者都包含在内?
回答by Nick Craver
I suppose this should be updated now that the landscape has changed quite a bit:
我想这应该更新,因为景观已经发生了很大的变化:
For an HTML5 doctype, it's no longer required. For example: we leave it out of the pages here at Stack Overflow. If you're using an HTML5 doctype then it's completely optional and defaults to text/javascript, so you're absolutely fine leaving it off in every current (or even very old) browser. Realistically, this was also true even in HTML4 though not strictly valid HTML.
对于 HTML5 文档类型,不再需要它。例如:我们将它排除在 Stack Overflow 的页面之外。如果您使用的是 HTML5 文档类型,那么它是完全可选的并且默认为text/javascript,因此您绝对可以在每个当前(甚至非常旧的)浏览器中将其关闭。实际上,即使在 HTML4 中也是如此,尽管不是严格有效的 HTML。
For an HTML4 doctype (to be valid), you need it. For a browser to actually function, it's not strictly needed and will behave just fine (this has been true all the way back to Netscape 2) - but it won't be valid HTML. If you have an HTML4 doctype, then keep it around and be valid - cause hey, why not?
对于 HTML4 文档类型(有效),您需要它。对于实际运行的浏览器来说,它并不是严格需要的,并且会表现得很好(从 Netscape 2 开始就是如此)——但它不会是有效的 HTML。如果你有一个 HTML4 文档类型,那么保留它并保持有效 - 因为嘿,为什么不呢?
Original Answer:
原答案:
I would use type="text/javascript"to be safe in all current browsers, why leave the ambiguity in there to save 21 characters? language=""however is deprecated, I'd leave it out.
我type="text/javascript"以前在所有当前浏览器中都是安全的,为什么要在那里留下歧义以保存 21 个字符? language=""但是不推荐使用,我会忽略它。
Also, any validator is going to throw an error, though it will likely work inside the browser (unless you're dealing with something veryold).
此外,任何验证器都会抛出错误,尽管它可能会在浏览器中工作(除非您处理的是非常旧的东西)。
回答by Justin Ethier
回答by Jacob
The W3C recommendation for HTML5 says that you do not need to include
W3C 对 HTML5 的建议说您不需要包含
type="text/javascript"
type="text/javascript"
The browser assumes that it is text/javascript unless otherwise stated as a different type.
除非另有说明,否则浏览器会假定它是 text/javascript。
http://dev.w3.org/html5/spec/Overview.html#the-script-block-s-type
http://dev.w3.org/html5/spec/Overview.html#the-script-block-s-type
回答by brainjam
Douglas Crockford, one of the great authorities and teachers of Javascript, has this to say:
Douglas Crockford是 Javascript 的伟大权威和教师之一,他说:
language="javascript"This attribute has been deprecated. It was used to select other programming languages and specific versions of JavaScript. You don't need it. Don't use it.
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.
language="javascript"此属性已被弃用。它用于选择其他编程语言和 JavaScript 的特定版本。你不需要它。不要使用它。
type="text/javascript"该属性是可选的。从 Netscape 2 开始,所有浏览器的默认编程语言都是 JavaScript。在 XHTML 中,此属性是必需的,也是不必要的。在 HTML 中,最好将其排除在外。浏览器知道该做什么。
Your boss may be doing this for the "right" or the "wrong" reasons (i.e. he may be following Crockford's advice, or he may just be lazy), but I don't think you can necessarily make a judgement. If the rest of his HTML and JS is sloppy, that's another thing. I'd venture that the contents of the script tag could be more of a religious thing like tab size or brace placement.
你的老板这样做可能是出于“正确”或“错误”的原因(即他可能听从 Crockford 的建议,或者他可能只是懒惰),但我认为你不一定能做出判断。如果他的 HTML 和 JS 的其余部分是草率的,那是另一回事。我敢说脚本标签的内容可能更像是标签大小或括号放置等宗教内容。
Edit:@coffeeaddict has pointed out that not putting the proper attributes into the tag messes up his compiles. I'd say that trumps any consideration of whether the attributes are strictly correct or necessary, because projects should always build cleanly without errors or warnings. Same goes for validators, etc, if they are part of the project standard.
编辑:@coffeeaddict 指出没有将正确的属性放入标签会弄乱他的编译。我想说的是,考虑属性是否严格正确或必要,这比任何考虑都重要,因为项目应该始终构建干净,没有错误或警告。如果验证器等是项目标准的一部分,则它们也是如此。
回答by Dustin
If the document is parsed as HTML5, the language will default to JavaScript, and no attribute of any sort is required (for future reference, there is no language attribute in HTML5).
如果文档被解析为 HTML5,语言将默认为 JavaScript,并且不需要任何类型的属性(以供将来参考,HTML5 中没有语言属性)。
If you're catering to HTML 4.x or XHTML 1.x, the default scripting language is supposedly determined from the value of the Content-Script-Type header, whether present locally in a META/meta tag (high priority) or as an HTTP header (low priority). The type attribute is still required by HTML 4.x even if the Content-Script-Type header is present (locally or otherwise) since the default scripting language only affects how the values of attributes like onload, onclick, etc. are handled. The type attribute with "text/javascript" as the value ought to be used in the case of JavaScript instead of the language attribute unless you're catering to old browsers (e.g. IE4, NN4, perhaps IE5/Mac? ; remember that there was a version of IE6 for Windows 98, so the language attribute is definitely outdated enough).
如果您要迎合 HTML 4.x 或 XHTML 1.x,则默认脚本语言应该由 Content-Script-Type 标头的值确定,无论是本地存在于 META/meta 标记中(高优先级)还是作为HTTP 标头(低优先级)。即使存在 Content-Script-Type 标头(本地或其他方式),HTML 4.x 仍然需要 type 属性,因为默认脚本语言仅影响 onload、onclick 等属性值的处理方式。除非您要迎合旧浏览器(例如 IE4、NN4,也许 IE5/Mac?),否则应该在 JavaScript 的情况下使用以“text/javascript”作为值的类型属性而不是语言属性IE6 for Windows 98 的一个版本,所以语言属性肯定已经过时了)。
One last bit of information: technically speaking, application/x-javascript is the correct value for JavaScript (unless it became application/javascript without me knowing), but unfortunately text/javascript is the one with the greatest support in terms of cross-browser compatibility.
最后一点信息:从技术上讲,application/x-javascript 是 JavaScript 的正确值(除非它在我不知道的情况下变成 application/javascript),但不幸的是 text/javascript 是跨浏览器支持最大的那个兼容性。
回答by Stefan Kendall
ASP.NET has nothing to do with anything. xhtml 1.0 dictates you use type="text/javascript", with quotes, or else you're not producing valid xhmtl.
ASP.NET 与任何事情无关。xhtml 1.0 规定您使用带有引号的 type="text/javascript",否则您将无法生成有效的 xhmtl。
Run the w3c validatoragainst your pages, and please comply with what it asks.
对您的页面运行w3c 验证器,请遵守它的要求。

