charset=iso-8859-1 with <!DOCTYPE HTML> 抛出警告?

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

charset=iso-8859-1 with <!DOCTYPE HTML> throwing a warning?

htmlcharacter-encodingdoctypehtml-validation

提问by ajax333221

I just validated a html doc using the W3-validator, and found that If I use:

我刚刚使用W3-validator验证了一个 html 文档,发现如果我使用:

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

with:

和:

<!DOCTYPE HTML>
  • It throws a warning Line 4, Column 72: Using windows-1252 instead of the declared encoding iso-8859-1.
  • 它发出警告 Line 4, Column 72: Using windows-1252 instead of the declared encoding iso-8859-1.

However, it is fixed if I use:

但是,如果我使用它是固定的:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

I don't really understand what is happening, also I don't even know how to use the DOCTYPE tag, I just copied and pasted one from around the web.

我真的不明白发生了什么,我什至不知道如何使用 DOCTYPE 标签,我只是从网上复制并粘贴了一个。

Can someone point me in the right direction to understand:

有人可以指出我正确的方向来理解:

  • why this happens
  • and, how to use the DOCTYPE tag
  • 为什么会发生这种情况
  • 以及,如何使用 DOCTYPE 标签

采纳答案by Andrew Stewart

Couple points:

几点:

  1. Any HTML5 validation should be taken with a grain of salt. The spec is still under active development, and not everything is set in stone.
  2. You're using the HTML4 syntax for that meta tag. Try <meta charset="iso-8859-1">
  1. 任何 HTML5 验证都应该有所保留。该规范仍在积极开发中,并非一切都是一成不变的。
  2. 您正在为该元标记使用 HTML4 语法。尝试<meta charset="iso-8859-1">

That said, HTML validators don't serve that much purpose in this day and age.

也就是说,HTML 验证器在当今时代并没有那么多用途。

Also, why do you need to specify that particular charset?

另外,为什么需要指定该特定字符集?

EDIT:

编辑

My bad, apparently the default for HTML4 was iso=8869-1. That said, the default charset for HTML5 is utf-8.

我的不好,显然 HTML4 的默认值是 iso=8869-1。也就是说,HTML5 的默认字符集是 utf-8。

More information about the HTML5 doctype can be found in this postby John Resig

有关 HTML5 文档类型的更多信息可以在John Resig 的这篇文章中找到

回答by Alohci

Changing the DOCTYPE is simply turning off the warning - it isn't actually fixing anything.

更改 DOCTYPE 只是关闭警告 - 它实际上并没有解决任何问题。

iso-8859-1and windows-1252are very similar encodings. They differ only in the characters associated with the 32 byte values from 0x80 to 0x9F, which in iso-8859-1are mapped to control characters and in windows-1252are mapped to some useful characters such as the Euro symbol.

iso-8859-1并且windows-1252是非常相似的编码。它们的区别仅在于与从 0x80 到 0x9F 的 32 字节值相关联的字符,这些iso-8859-1字符被映射到控制字符,而 inwindows-1252被映射到一些有用的字符,例如欧元符号。

The control characters are useless in HTML, and web authors often mistakenly declare iso-8859-1and yet use one or more of those 32 values as if they were using windows-1252, so browsers when they see the iso-8859-1charset being declared will automatically change this to be windows-1252.

控制字符在 HTML 中是无用的,网络作者经常错误地声明iso-8859-1并使用这 32 个值中的一个或多个,就好像他们在使用 一样windows-1252,因此浏览器在看到iso-8859-1声明的字符集时会自动将其更改为windows-1252.

The validator is simply warning you that this will happen. If you're not using any of the 32 byte values, then you can simply ignore the warning - it's NOT an error. If you are, and you genuinely want the iso-8859-1interpretation of the byte values and not the windows-1252interpretation, you are doing something wrong.

验证器只是警告您这会发生。如果您没有使用任何 32 字节值,那么您可以简单地忽略警告 - 这不是错误。如果你是,并且你真的想要iso-8859-1字节值的windows-1252解释而不是解释,那么你做错了。

Again, this switching happens in browsers for any DOCTYPE, it's just that the HTML5 validator is being more helpful about what it is telling you than the HTML4 validator is.

同样,这种切换发生在任何 DOCTYPE 的浏览器中,只是 HTML5 验证器比 HTML4 验证器更能帮助您了解它告诉您的内容。

回答by delroh

"Using windows-1252 instead of the declared encoding iso-8859-1." It means the file was saved with the encoding windows 1252 on creation (aka Western Windows 1252 or cp1252) and your charset declaration says "hey read this file with iso-8859-1" when that's not the encoding the file has.

“使用 windows-1252 而不是声明的编码 iso-8859-1。” 这意味着该文件在创建时使用编码 windows 1252(又名 Western Windows 1252 或 cp1252)保存,并且您的字符集声明说“嘿,用 iso-8859-1 读取此文件”,当这不是文件的编码时。

The meta charset exist for that reason. It exist to declare the encoding of the file you are sending/reading/using so when, for example a browser, reads the document it knows what encoding the file is using.

元字符集就是因为这个原因而存在的。它的存在是为了声明您正在发送/阅读/使用的文件的编码,因此,例如,当浏览器读取文档时,它知道文件正在使用什么编码。

In detail, you have this charset declared:

详细地说,您声明了此字符集:

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

But the file you are validating is actually encoded in Windows 1252. How? Why? Check the text editor you are using and what encoding it is using to save files. If the editor can be configured to change the encoding, choose the one you want to use.

但是您正在验证的文件实际上是在 Windows 1252 中编码的。如何?为什么?检查您正在使用的文本编辑器以及它用于保存文件的编码。如果编辑器可以配置为更改编码,请选择您要使用的编码。

About HTML5

关于 HTML5

Using

使用

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

or

或者

<meta charset="iso-8859-1">

are both valid for HTML5. See <meta charset="utf-8"> vs <meta http-equiv="Content-Type">

都对 HTML5 有效。参见<meta charset="utf-8"> 与 <meta http-equiv="Content-Type">

回答by novato

Do the follow:

执行以下操作:

ISO 8859-15 yeah -15 and it will work. I know this answer is new and the question is old but the idea is that future users comming from search engines like me gets the correct answer

ISO 8859-15 是 -15,它会起作用。我知道这个答案是新的,问题是旧的,但想法是像我这样来自搜索引擎的未来用户会得到正确的答案

回答by Roger F. Gay

I can see this is an old question, but thought it better to provide an updated answer. Maybe I've noticed something others haven't (after encountering the same problem and thus finding this post before working it out myself).

我可以看到这是一个老问题,但我认为最好提供更新的答案。也许我已经注意到其他人没有注意到的东西(在遇到同样的问题之后,因此在自己解决之前找到了这篇文章)。

The W3C validator offers options for which encoding the validator uses. You have specified encoding in your document, so you should see "Encoding: iso-8859-1" in the top block of information once the validator has been run. To the right of that, there is a pull-down menu. Change the choice from "(detect automatically)" to "iso-8859-1 (Western European)". The validator will then use iso-8859-1 instead of its own choice, and you will not receive the error.

W3C 验证器提供了验证器使用的编码选项。您已在文档中指定了编码,因此一旦运行验证器,您应该会在顶部信息块中看到“编码:iso-8859-1”。在它的右侧,有一个下拉菜单。将选择从“(自动检测)”更改为“iso-8859-1(西欧)”。然后验证器将使用 iso-8859-1 而不是它自己的选择,您将不会收到错误消息。

回答by JoeL

Don't place too much stock in the validators. There are typically too many Internet Explorer work-arounds, particularly in the css, that will trip up the validator. If your pages work in all browsers and your client is happy, does it matter what some validator says?

不要在验证器中放置太多库存。通常有太多的 Internet Explorer 变通方法,特别是在 css 中,会导致验证器失败。如果您的页面在所有浏览器中都可以运行并且您的客户很满意,那么某些验证器所说的是否重要?

If you are specifying the html5 doctype, then you should be consistent with the meta charset attribute. Try this though for your pages:

如果您指定的是 html5 doctype,那么您应该与元字符集属性保持一致。为您的页面尝试此操作:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
</head>

<body>
</body>
</html>