Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting charset for a specific div

Is it possible to assign a charset for at specific div? So that you can have more than one charset on a page.

I'm currently importing snippets of text to my site via JS, and some of this text requires the UTF-8 charset. To be sure that my text is shown right on every page it is included (Sometimes external sites), I force the metatag into all the sites.

Is it possible to apply this charset to only a specific div, span or something like that?

like image 535
Squazz Avatar asked Oct 07 '14 12:10

Squazz


People also ask

What does charset UTF-8 do in HTML?

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!

How do I specify character encoding in HTML?

Always declare the encoding of your document using a meta element with a charset attribute, or using the http-equiv and content attributes (called a pragma directive).

Is meta charset UTF-8 needed?

Furthermore, most browsers use UTF-8 by default if no character encoding is specified. But because that's not guaranteed, it's better to just include a character encoding specification using the <meta> tag in your HTML file. There you have it.

What is the difference between UTF-8 and ISO 8859 1?

UTF-8 is a multibyte encoding that can represent any Unicode character. ISO 8859-1 is a single-byte encoding that can represent the first 256 Unicode characters. Both encode ASCII exactly the same way.


1 Answers

No it is not, and it also entirely unnecessary.

The <meta> element declaring a charset or, better, the equivalent HTTP header is only there to help the browser correctly interpret the HTML text. Once the browser has done so, it constructs a DOM out of it and you may essentially treat the text as having no concrete charset after this point. For all intends and purposes the text exists as text in the DOM, not as binary representation which must be interpreted by a charset decoder.

When you're adding new content to the DOM via Javascript, the same ideas apply. The browser needs to fetch the new content via HTTP and the content's encoding should be denoted by an HTTP header. The browser can convert the text from the specific encoding to "DOM text" based on that, after which is doesn't matter anymore what encoding it was in.

Therefore, you can perfectly mix and match encodings from different sources being delivered in separate HTTP responses within the same page/DOM without having to worry about a "global" encoding.

like image 106
deceze Avatar answered Sep 18 '22 16:09

deceze