简体   繁体   中英

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!

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

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, eg <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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM