Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of charset in the script tag for Javascript

I need to include text of different languages in the javascript app that I'm writing, and the characters need to show up properly in different webpages that have different page encodings. The script is external.

I've read that in order to have the browser correctly interpret and display these characters, a charset tag should be added in the script tag. However, I am facing a few problems with this approach and would like to see if anyone could help.

  1. The order of the charset seems to matter in IE9. Should "charset" be declared before "src" in the script tag? Is there such a specification or did anyone see a similar issue?

  2. I've seen someone comment in another forum that the charset in an element will not be processed if the webpage defines a charset in the http content type/meta (or something along that line). Is this true? Does this mean that the charset in the script tag will be ignored then? If so, how to overcom

  3. Are there any other recommended methods besides using charset in the script tag? Manipulating the webpages is not an option.

like image 607
muffin Avatar asked Dec 01 '11 04:12

muffin


People also ask

What does charset mean in JavaScript?

Definition and Usage The charset attribute specifies the character encoding for the HTML document.

What is the purpose of charset?

The purpose of the charset parameter is to specify the encoding of the external script in cases where the encoding is not specified at the HTTP protocol level. It is not meant to override encoding information in HTTP headers, and it does not do that.

What is the use of script tag in JavaScript?

The <script> tag is used to embed a client-side script (JavaScript). The <script> element either contains scripting statements, or it points to an external script file through the src attribute. Common uses for JavaScript are image manipulation, form validation, and dynamic changes of content.

What character encoding does JavaScript use?

Most JavaScript engines use UTF-16 encoding, so let's detail into UTF-16. UTF-16 (the long name: 16-bit Unicode Transformation Format) is a variable-length encoding: Code points from BMP are encoded using a single code unit of 16-bit. Code points from astral planes are encoded using two code units of 16-bit each.


1 Answers

If I understood you correctly, you have multiple js files each one using a different encoding, and you tried the charset on the <script> tag but that is causing you troubles.

If so, have you tried not adding the charset at all and having the javascript files encoded in Unicode? I found that to be the best in having the browser actually render the correct fonts.

read this for some examples and let us know if it works out for you!

like image 140
Leon Avatar answered Oct 01 '22 17:10

Leon