从 Javascript 动态加载字符集 UTF-8 和 ISO-8859-1 [英] Dynamically load the charset UTF-8 and ISO-8859-1 from Javascript
问题描述
我尝试使用字符集 UTF-8 来显示 ä,它显示了一些方框.
I tried charset UTF-8 to display the ä, it displayed some square box.
我也尝试使用字符集 ISO-8859-1 来显示 ä,它显示为 ä.(这是正确的)
Also i tried with charset ISO-8859-1 to display the ä, it diplayed as ä. (which is correct)
但是当在javascript条件下结合上述两个字符集时,它不能正常工作.参考下面的代码,
But When combine the above both charset within javascript condition, its not working properly. Refer below code,
<html>
<head>
<script type="text/javascript">
var lang = 'German';
function f(){
if(lang != 'SomeOtherLanguage'){
//here code will execute. And page should display square box. Instead of square box, ä is displayed. Which is wrong. I cant able to find reason.
metaTag = '<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>';
}
else
metaTag = '<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>';
document.getElementsByTagName('head')[0].innerHTML += metaTag;
}
</script>
</head>
<body onload="f()">
<h1>Latin letter : ä </h1> <br />
</body>
</html>
推荐答案
你试图做的事情永远不会奏效.
What you are attempting to do will never work.
如果您的 HTML 的原始字节开始时未编码为 UTF-8,则您不能在 <meta>
标记或 HTTP 中声明 UTF-8>Content-Type
标题.你会对浏览器/客户端撒谎,这就是为什么你会得到糟糕的结果.
If the raw bytes of your HTML are not encoded as UTF-8 to begin with, you can't claim UTF-8, in a <meta>
tag, or an HTTP Content-Type
header. You would be lying to the browser/client, and is why you get bad results.
只有当您的 <meta>
标签声明 ISO-8859-1(并且没有 Content-Type
标头来覆盖它)时,您的代码才会工作"如果您的 HTML 实际上是用 ISO-8859-1 编码的.在包括 ISO-8859-1 在内的几个(但不是全部)ISO-8859-X 字符集中,ä
被编码为字节 0xE4
,因此您的代码有效"如果 HTML 的原始数据中存在字节 0x34
,则声明 ISO-8859-1 时.
Your code will "work" only when your <meta>
tag claims ISO-8859-1 (and there is no Content-Type
header to override that) if your HTML is actually encoded in ISO-8859-1. In several (but not all) of the ISO-8859-X charsets, including ISO-8859-1, ä
is encoded as byte 0xE4
, so your code "works" when claiming ISO-8859-1 if byte 0x34
is present in the HTML's raw data.
在 UTF-8 中,ä
被编码为字节 0xC3 0xA4
.如果您的 HTML 包含字节 0xE4
,但您声明为 UTF-8,则会得到不好的结果(0xE4
不是 UTF-8 中的有效字节).
In UTF-8, ä
is encoded as bytes 0xC3 0xA4
instead. If your HTML contains byte 0xE4
, but you claim UTF-8, you get bad results (0xE4
is not a valid byte in UTF-8).
因此,您的 标记(和 HTTP
Content-Type
标头)需要声明一个真正与 HTML 原始字节的真实编码相匹配的字符集.
So, your <meta>
tag (and HTTP Content-Type
header) needs to claim a charset that actually matches the real encoding of the HTML's raw bytes.
如果您的 HTTP 服务器正在提供静态 HTML 文件,则在将 HTML 保存到文件时,该文件会以特定字符集进行编码.需要在 <meta>
标记中静态指定相同的字符集(最好也在 HTTP Content-Type
标头中).如果您的 HTTP 服务器正在动态生成 HTML,则需要将 HTML 编码为特定字符集以进行传输,因此需要在生成的 标记(和
Content-Type
标头).
If your HTTP server is serving a static HTML file, the file is encoded in a specific charset when the HTML is saved to file. That same charset needs to be specified statically in the <meta>
tag (and preferably also in the HTTP Content-Type
header). If your HTTP server is generating the HTML dynamically, it needs to encode the HTML in a specific charset for transmission, so it needs to specify that same charset in the generated <meta>
tag (and Content-Type
header).
换句话说,不要再试图欺骗浏览器/客户端.说实话,以后就不会再遇到这个问题了.
In other words, stop trying to lie to the browser/client. Tell the truth, then you won't run into this problem anymore.
这篇关于从 Javascript 动态加载字符集 UTF-8 和 ISO-8859-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!