Javascript character encoding

In the external javascript file, I have a function that is used to add text to table cells (in the HTML document to which the javascript file is added), text that can sometimes have Finnish characters (for example, ä). This text is passed as an argument to my function:

content += addTableField(XML, 'Käyttötarkoitus', 'purpose', 255); 

The problem is that diacritics such as "ä" are converted to some other dummy characters, such as "". I see this when viewing an HTML document in a browser. This is clearly undesirable, and it is rather strange, since the character encoding for the HTML document is UTF-8.

How can I solve this problem?

Thanks in advance for your help!

+4
source share
3 answers

File containing content += addTableField(XML, 'Käyttötarkoitus', 'purpose', 255); is not stored in UTF-8 encoding.

I don’t know which editor you are using, but you can find it in the settings or in the save dialog.

Example:

If you cannot make this work, you can always write literal codes in javascript:

 content += addTableField(XML, 'K\u00E4ytt\u00f6tarkoitus', 'purpose', 255); 

credit: triplee

+4
source

To check the character encoding declared by the server, you can use Firebug (in the Info menu, theres to view HTTP headers). In addition, you can use online services such as Web-Sniffer .

If the charset parameter is specified in the headers of the external .js file, you need to use this encoding if you cannot change the corresponding server settings (possibly the .htaccess file).

If they do not have the charset parameter, you can specify the encoding in the script element, for example. <script src="foo.js" charset="utf-8"> .

The declared encoding should, of course, correspond to the actual encoding, which you can usually choose when saving the file (if necessary, use the "Save As" command).

+1
source

The character encoding of an HTML file / document has nothing to do with an external source.

You will need to deliver a script file with UTF8 character encoding. If it was saved as such, your server configuration is dummy.

0
source

Source: https://habr.com/ru/post/1415205/


All Articles