简体   繁体   English

为什么是<meta HTTP-EQUIV="“Content-Script-Type”" …>大部分未使用?

[英]Why is <META HTTP-EQUIV=“Content-Script-Type” …> mostly unused?

Related: What's the point of Content-Script-Type and Content-Style-Type .相关: Content-Script-Type 和 Content-Style-Type 有什么意义

I wanted to know what the main reasons are that developers don't use我想知道开发人员不使用的主要原因是什么

  • <meta http-equiv="content-script-type" content="text/javascript" /> and <meta http-equiv="content-script-type" content="text/javascript" />
  • <meta http-equiv="content-style-type" content="text/css" />

in their web projects.在他们的 web 项目中。 (Me neither, btw.) (我也没有,顺便说一句。)

Saving one from having to declare the used type on every instance of <script> and <style> , it does not seem to have any drawbacks.不必在<script><style>的每个实例上声明使用的type ,它似乎没有任何缺点。 Yet, in fact, I have never seen one of them in the wild.然而,事实上,我从未在野外见过它们中的任何一个。 Are there any considerations one has to take when relying on these <meta> tags?依赖这些<meta>标签时,是否需要考虑任何因素?

According to W3C , http-equiv values "content-style-type" & "content-script-type" attributes are unknown for HTML5 meta markup, Moreover: W3C validator throws the following error when an HTML5 page has such markups:根据W3C ,对于 HTML5 元标记,http-equiv 值“content-style-type”和“content-script-type”属性是unknown的,此外:当 HTML5 页面具有此类标记时,W3C 验证器会引发以下错误:

Line X, Column Y: Bad value Content-Script-Type for attribute http-equiv on element meta. X 行,Y 列:元素元上属性 http-equiv 的错误值 Content-Script-Type。

<meta http-equiv="Content-Script-Type" content="text/javascript">

So essentially we are supposed to avoid them.所以本质上我们应该避免它们。

All web browsers that I've heard of will default to assuming type="text/javascript" on all <script> tags, and type="text/css" on <style> tags.我听说过的所有 web 浏览器都默认在所有<script>标签上假设type="text/javascript" ,在<style>标签上假设 type= type="text/css" (The only meaningful alternative I've heard of is VBScript for <script> tags in MSIE, which is heavily deprecated. There's no alternative to CSS.) In recognition of this, the HTML5 spec defines both attributes as being newly optional. (我听说的唯一有意义的替代方案是 MSIE 中<script>标记的 VBScript,它已被严重弃用。CSS 没有替代方案。)认识到这一点,Z6BFF62B10D884FBFEDZ 规范将两个属性定义为新的

As such, there's no point in the Content-Script-Type and Content-Style-Type meta tags -- as far as I'm aware, they're ignored by most, if not all, browsers.因此, Content-Script-TypeContent-Style-Type元标记没有意义——据我所知,大多数(如果不是全部)浏览器都会忽略它们。

It's a shame that these settings have become deprecated with HTML5.遗憾的是,这些设置已被 HTML5 弃用。 Because, as nobody else seems to be mentioning, you can put default character set settings in there as well: Thus:因为,正如其他人似乎没有提到的那样,您也可以将默认字符集设置放在那里:因此:

<meta http-equiv="Content-Script-Type" content="text/javascript; charset=UTF-8;"> <meta http-equiv="Content-Style-Type" content="text/css; charset=UTF-8;"> <meta http-equiv="Content-Script-Type" content="text/javascript; charset=UTF-8;"> <meta http-equiv="Content-Style-Type" content="text/css; charset=UTF-8;">

<meta http-equiv="Content-Style-Type" content="text/css; charset=UTF-8;">

The CSS meta is important for inline styles where we can't declare the type, so: CSS 元数据对于我们无法声明类型的内联 styles 很重要,因此:

<span style="background:pink">

Speaking as a developer who just learned about these tags from your question, I'd say that it's the curse of the legacy browsers (I'm looking at you, IE6).作为刚刚从您的问题中了解这些标签的开发人员,我想说这是旧版浏览器的诅咒(我在看着您,IE6)。 Because when I learn about new tags, I usually continue not using them.因为当我了解新标签时,我通常会继续不使用它们。 I always assume browsers might not support any feature that I've never heard of, until I prove otherwise (which takes time), and since you have to program to the least common denominator (even if you "progressively enhance" later), that means, in this case, using the safer, more verbose method.我总是假设浏览器可能不支持我从未听说过的任何功能,直到我证明不是这样(这需要时间),并且由于您必须按照最小公分母进行编程(即使您稍后“逐步增强”),那在这种情况下,意味着使用更安全、更详细的方法。

Having said that, I may actually give these a try.话虽如此,我实际上可以尝试一下。 There's little risk, unless you're using content types other than text/javascript and text/css , since those have been the assumed defaults, like, forever.除非您使用text/javascripttext/css以外的内容类型,否则几乎没有风险,因为这些都是假定的默认值,就像永远一样。 Indeed, as @duskwuff points out, there's probably no point in using either.事实上,正如@duskwuff 指出的那样,使用任何一个都可能没有意义。

To add to the discussion.....添加到讨论中......

  1. Validators for HTML5 will reject these metatag formats as they are deprecated, as some here mentioned. HTML5 的验证器将拒绝这些元标记格式,因为它们已被弃用,正如这里提到的一些。 If you care about strict HTML5 validation then you will have to remove them.如果您关心严格的 HTML5 验证,那么您将不得不删除它们。 Because the browser defaults are close to what older browsers used, you will likely be ok without them.因为浏览器默认值与旧浏览器使用的很接近,所以没有它们你可能会没事。 I still use them for older browser support.我仍然使用它们来支持旧的浏览器。

  2. HTML5 browsers also use "application/javascript" (not "text/javascript" ) as the correct script type, which older browsers might not support. HTML5 浏览器也使用"application/javascript" (不是"text/javascript" )作为正确的脚本类型,旧版浏览器可能不支持。 Again, I would use the older metatag with "text/javascript" just to be safe.同样,为了安全起见,我会使用带有"text/javascript"的旧元标记。

  3. "http-equiv" is a replacement for what the server sends to the browser. “http-equiv”是服务器发送给浏览器的替代品。 My understanding is the metatags will be overwritten by what the server says.我的理解是元标记将被服务器所说的覆盖。 But use them if you are worried about missing server info, though the modern browser defaults would likely be enough.但是,如果您担心丢失服务器信息,请使用它们,尽管现代浏览器默认值可能就足够了。 If a server did send "Content-Script-Type" and "Content-Style-Type" then you would not need them.如果服务器确实发送了"Content-Script-Type""Content-Style-Type" ,那么您将不需要它们。

  4. What if you use these metatags because your "inline" CSS and JavaScript is in a web page that was encoded differently from the HTML5 default of UTF-8? What if you use these metatags because your "inline" CSS and JavaScript is in a web page that was encoded differently from the HTML5 default of UTF-8? My understanding is JavaScript is always stored as UTF-16, but parsed from UTF-8 encoded pages which is the default for HTML5.我的理解是 JavaScript 始终存储为 UTF-16,但从 UTF-8 编码页面解析,这是 HTML5 的默认值。 Many older HTML files are likely saved as ASCII or UTF-8, as well, but could be something else.许多较旧的 HTML 文件也可能保存为 ASCII 或 UTF-8,但也可能是其他文件。 Most JavaScript parsers, old and new, navigate all those scenarios and sniffs the right encoding, using your metatags as one of many hints to find out what type and encoding the code uses.大多数 JavaScript 解析器,无论新旧,都会导航所有这些场景并嗅探正确的编码,使用您的元标记作为找出代码使用的类型和编码的众多提示之一。 Many do this regardless of how you encode your files or pages, or what metatag settings are used.无论您如何编码文件或页面,或者使用什么元标记设置,许多人都会这样做。 So if you do encode HTML pages or files differently, it might not matter what the metatag says.因此,如果您确实对 HTML 页面或文件进行了不同的编码,那么元标记的内容可能并不重要。 But it doesn't hurt.但它不疼。 Unless you include higher level UNICODE/languages in your CSS or scripts, you don't need to worry about changing the encoding or using metatags to control that.除非您在 CSS 或脚本中包含更高级别的 UNICODE/语言,否则您无需担心更改编码或使用元标记来控制它。

Being HTTP-EQUIV - this has little to do with being deprecated in HTML5 (because here indeed the defaults are JS and CSS - which also makes the type-attribute unnecessary) - there is still the HTTP protocol: https://www.ietf.org/rfc/rfc4229.txt#2.1.30 Being HTTP-EQUIV - this has little to do with being deprecated in HTML5 (because here indeed the defaults are JS and CSS - which also makes the type-attribute unnecessary) - there is still the HTTP protocol: https://www.ietf .org/rfc/rfc4229.txt#2.1.30

so you can from the serverside very well send HTTP headers eg from PHP所以你可以从服务器端很好地发送 HTTP 标头,例如来自 PHP

<?php
header('Content-Type: text/html; charset=UTF-8');
header('Content-Script-Type: text/javascript');
header('Content-Style-Type: text/css');

or node或节点

res.set('Content-Type', 'text/html; charset=UTF-8');
res.set('Content-Script-Type', 'text/javascript');
res.set('Content-Style-Type', 'text/css');

They are depreciated.它们是折旧的。 Now, people use <script type='text/javascript> and <style type='text/css'> .现在,人们使用<script type='text/javascript><style type='text/css'>

<script type="text/javascript">

is depricated, so use被贬低,所以使用

<script type="application/javascript">

instead like mentioned here in april 2006 .而不是像2006年 4 月在这里提到的那样。 Start here to find the last content.从这里开始查找最后的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 <meta charset="utf-8">对比<meta http-equiv="Content-Type"> - <meta charset="utf-8"> vs <meta http-equiv="Content-Type"> 什么是<meta http-equiv=“Content-Type” content=“text/html; charset=utf-8” /> ? - What is <meta http-equiv=“Content-Type” content=“text/html; charset=utf-8” />? 我应该避免在html中使用http-equiv =“ content-type”元标记吗? - Should I avoid the usage of the http-equiv=“content-type” meta tag in html? Meta标签解释:http-equiv和charset - Meta tags explained: http-equiv and charset 元http-equiv在头外? - meta http-equiv outside of head? HTML meta http-equiv未显示 - HTML meta http-equiv not showing up <meta http-equiv="refresh" content="0; url=http://www.xxxxxxl.com/index.php" />不清爽 - <meta http-equiv="refresh" content="0; url=http://www.xxxxxxl.com/index.php" /> is not refreshing 内容页面<meta http-equiv=“X-UA-Compatible” content=“IE=10”/> - Content page with <meta http-equiv=“X-UA-Compatible” content=“IE=10”/> 是 <html lang=“de-DE”> 等于 <meta http-equiv=“language” content=“DE”> ? - Is <html lang=“de-DE”> equal to <meta http-equiv=“language” content=“DE”>? 如何停止 <META HTTP-EQUIV=“refresh” CONTENT=“2”> N次刷新页面后 - how to stop <META HTTP-EQUIV=“refresh” CONTENT=“2”> after N-times of refreshing the page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM