Andrei Oniga
Andrei Oniga

Reputation: 8559

Javascript character encoding

In an external javascript file I have a function that is used to append text to table cells (within the HTML doc that the javascript file is added to), text that can sometimes have Finnish characters (such as ä). That text is passed as an argument to my function:

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

The problem is that diacritics such as "ä" get converted to some other bogus characters, such as "�". I see this when viewing the HTML doc in a browser. This is obviously not desirable, and is quite strange as well since the character encoding for the HTML doc is UTF-8.

How can I solve this problem?

Thanks in advance for helping out!

Upvotes: 4

Views: 8763

Answers (3)

Jukka K. Korpela
Jukka K. Korpela

Reputation: 201568

To check out the character encoding announced by a server, you can use Firebug (in the Info menu, there’s a command for viewing HTTP headers). Alternatively, you can use online services like Web-Sniffer.

If the headers for the external .js file specify a charset parameter, you need to use that encoding, unless you can change the relevant server settings (perhaps a .htaccess file).

If they lack a charset parameter, you can specify the encoding in the script element, e.g. <script src="foo.js" charset="utf-8">.

The declared encoding should of course match the actual encoding, which you can normally select when you save a file (using “Save As” command if needed).

Upvotes: 1

Esailija
Esailija

Reputation: 140220

The file that contains content += addTableField(XML, 'Käyttötarkoitus', 'purpose', 255); is not saved in UTF-8 encoding.

I don't know what editor you are using but you can find it in settings or in the save dialog.

Example:

If you can't get this to work you could always write out the literal code points in javascript:

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

credit: triplee

Upvotes: 4

Bergi
Bergi

Reputation: 664513

The character encoding of the HTML file / doc does not matter any external ressource.

You will need to deliver the script file with UTF8 character encoding. If it was saved as such, your server config is bogus.

Upvotes: 0

Related Questions