简体   繁体   English

使用 JavaScript 设置字符集元标记

[英]Set charset meta tag with JavaScript

There's a bug I'm trying to track down here: https://github.com/OscarGodson/EpicEditor/issues/184#issuecomment-8805982我试图在这里追踪一个错误: https : //github.com/OscarGodson/EpicEditor/issues/184#issuecomment-8805982

Based on all the information it seems like it's because the browser is defaulting to the user's native charset (in this case, ISO-8859-1 ) and not UTF-8 like on my machine and others in the US.根据所有信息,似乎是因为浏览器默认使用用户的本机字符集(在本例中为ISO-8859-1 )而不是像我的机器和美国其他机器上的UTF-8 I'm guessing that a fix is to use HTML to force the encoding to UTF-8 with:我猜想修复是使用 HTML 强制编码为UTF-8

 <meta charset='utf-8'> 

or要么

<meta http-equiv='Content-Type' content='Type=text/html; charset=utf-8'>

However, the JS isn't working.但是,JS 不起作用。 In the first example:在第一个例子中:

charsetMetaTag = self.editorIframeDocument.createElement('meta');
charsetMetaTag.charset = 'utf-8';
self.editorIframeDocument.getElementsByTagName('head')[0].appendChild(charsetMetaTag);

I just get back the following injected into the DOM:我刚取回注入到 DOM 中的以下内容:

<meta>

And in the 2nd example the http-equiv isn't being set:在第二个示例中,没有设置http-equiv

charsetMetaTag = self.editorIframeDocument.createElement('meta');
charsetMetaTag['http-equiv'] = 'Content-Type';
charsetMetaTag['content'] = 'text/html; charset=utf-8';
self.editorIframeDocument.getElementsByTagName('head')[0].appendChild(charsetMetaTag);

I get back the following HTML:我得到以下 HTML:

<meta content="text/html; charset=utf-8">

Yes, I need to do this dynamically as im dynamically creating the iframes.This may not even be the issue, but this is what it's looking like.是的,我需要动态地执行此操作,因为我正在动态创建 iframe。这甚至可能不是问题,但这就是它的样子。 The only "hack" i can think of is somehow using innerHTML...我能想到的唯一“黑客”是以某种方式使用innerHTML ......

You can't set the charset content attribute by setting the charset property because they don't reflect each other.您无法通过设置 charset 属性来设置 charset 内容属性,因为它们不会相互反映。 In fact there is no property that reflects the charset content attribute.事实上,没有反映字符集内容属性的属性。

The http-equiv content attribute is reflected by the httpEquiv property so http-equiv 内容属性由 httpEquiv 属性反映,因此

 charsetMetaTag['httpEquiv'] = 'Content-Type';

would create the meta element correctly.将正确创建元元素。

But none of this matters.但这都不重要。 The character set is established by the parser, so constructing the meta element in JavaScript after the HTML has been parsed will have no effect on the character set of the document at all.字符集是由解析器建立的,所以在解析完 HTML 之后在 JavaScript 中构造元元素对文档的字符集完全没有影响。

As Alohci said, creating charset-related meta tags from JS won't have much effect on the current page.正如 Alohci 所说,从 JS 创建与字符集相关的元标记不会对当前页面产生太大影响。

In my usecase, I need to be able to serialize the current page as a string and save it to some backend.在我的用例中,我需要能够将当前页面序列化为字符串并将其保存到某个后端。 Appending a missing charset meta tag (if not present) is useful for such an usecase.附加缺少的字符集元标记(如果不存在)对于此类用例很有用。

As a side-node, don't forget that the charset metatags should be at the beginning of according to the HTML5 spec.作为侧节点,不要忘记根据 HTML5 规范,字符集元标签应该在开头 See this answer .看到这个答案 This simple detail has lead to an important bug in my app :)这个简单的细节导致了我的应用程序中的一个重要错误 :)

You should rather use:你应该使用:

document.head.insertBefore(charsetMetaTag,document.head.firstChild);

I agree with both @alohci and @sebastien-lorber answers.我同意@alohci 和@sebastien-lorber 的答案。

But just addressing your orginal issue with just getting但只是解决您的原始问题

<meta>

using the setAttribute method使用 setAttribute 方法

charsetMetaTag.setAttribute("charset", "UTF-8");

and following @sebastien-lorber suggestion, will output并遵循@sebastien-lorber 的建议,将输出

<meta charset="UTF-8">

as first child element of head作为 head 的第一个子元素

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM