Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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. I'm guessing that a fix is to use HTML to force the encoding to UTF-8 with:

 <meta charset='utf-8'> 

or

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

However, the JS isn't working. 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:

<meta>

And in the 2nd example the http-equiv isn't being set:

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:

<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. The only "hack" i can think of is somehow using innerHTML...

like image 282
Oscar Godson Avatar asked Sep 24 '12 06:09

Oscar Godson


People also ask

What is meta charset in Javascript?

Definition and Usage The charset attribute specifies the character encoding for the HTML document. The HTML5 specification encourages web developers to use the UTF-8 character set, which covers almost all of the characters and symbols in the world!

What does meta charset UTF-8 /> do?

In other words, <meta charset="utf-8"> tells the browser to use the utf-8 character encoding when translating machine code into human-readable text and vice versa to be displayed in the browser.

How do I set HTML to UTF-8?

The character encoding should be specified for every HTML page, either by using the charset parameter on the Content-Type HTTP response header (e.g.: Content-Type: text/html; charset=utf-8 ) and/or using the charset meta tag in the file.

Does UTF-8 need meta charset?

It doesn't matter which you use, but it's easier to type the first one. It also doesn't matter whether you type UTF-8 or utf-8 . You should always use the UTF-8 character encoding. (Remember that this means you also need to save your content as UTF-8.)


1 Answers

You can't set the charset content attribute by setting the charset property because they don't reflect each other. In fact there is no property that reflects the charset content attribute.

The http-equiv content attribute is reflected by the httpEquiv property so

 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.

like image 185
Alohci Avatar answered Sep 18 '22 14:09

Alohci