如果页面已经是 UTF-8,那么将 accept-charset="UTF-8" 添加到 HTML 表单有什么好处吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3719974/
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
Is there any benefit to adding accept-charset="UTF-8" to HTML forms, if the page is already in UTF-8?
提问by philfreo
For pages already specified (either by HTTP header, or by meta tag), to have a Content-Type with a UTF-8 charset... is there a benefit of adding accept-charset="UTF-8"
to HTML forms?
对于已经指定的页面(通过 HTTP 标头或元标记),具有带有 UTF-8 字符集的内容类型......添加accept-charset="UTF-8"
到 HTML 表单是否有好处?
(I understand the accept-charset
attribute is broken in IE for ISO-8859-1, but I haven't heard of a problem with IE and UTF-8. I'm just asking if there's a benefit to adding it with UTF-8, to help prevent invalid byte sequences from being entered.)
(我知道accept-charset
ISO-8859-1的属性在 IE 中被破坏,但我没有听说 IE 和 UTF-8 有问题。我只是问用 UTF-8 添加它是否有好处,以有助于防止输入无效的字节序列。)
采纳答案by bobince
If the page is already interpreted by the browser as being UTF-8, setting accept-charset="utf-8"
does nothing.
如果该页面已被浏览器解释为 UTF-8,则设置accept-charset="utf-8"
不会执行任何操作。
If you set the encoding of the page to UTF-8 in a <meta>
and/or HTTP header, it will be interpreted as UTF-8, unlessthe user deliberately goes to the View->Encoding menu and selects a different encoding, overriding the one you specified.
如果您在一个<meta>
和/或 HTTP 标头中将页面的编码设置为 UTF-8 ,它将被解释为 UTF-8,除非用户故意转到 View->Encoding 菜单并选择不同的编码,覆盖那个编码你指定的。
In that case, accept-encoding
would have the effect of setting the submission encoding back to UTF-8 in the face of the user messing about with the page encoding. However, this still won't work in IE, due the previous problems discussed with accept-encoding
in that browser.
在这种情况下,accept-encoding
当用户处理页面编码时,会将提交编码设置回 UTF-8。但是,由于之前accept-encoding
在该浏览器中讨论过的问题,这在 IE 中仍然不起作用。
So it's IMO doubtful whether it's worth including accept-charset
to fix the case where a non-IE user has deliberately sabotaged the page encoding (possibly messing up more on your page than just the form).
因此,IMO 怀疑是否值得accept-charset
修复非 IE 用户故意破坏页面编码的情况(可能在您的页面上搞砸的不仅仅是表单)。
Personally, I don't bother.
就我个人而言,我不打扰。
回答by jwueller
I did not encounter any problems using UTF-8 with IE (6+) or any other major browser out there. You need to make sure, that a UTF-8 meta tag is set (IE needs this) and that all your files are UTF-8 encoded (which means that the webserver sends UTF-8 headers). Then there should not be any problem if you omit accept-charset
.
我在 IE (6+) 或任何其他主要浏览器中使用 UTF-8 时没有遇到任何问题。您需要确保设置了 UTF-8 元标记(IE 需要这个)并且您的所有文件都是 UTF-8 编码的(这意味着网络服务器发送 UTF-8 标头)。那么如果省略accept-charset
.