views:

71

answers:

2

Example:

The following string is defined in a json.js file.

var test = "One complimentary entrée with the purchase of an entrée.";

It is included in an index.html file by

  <script type="text/JavaScript" src="./json.js"></script>

When the string is displayed in UI, it shows up as

"One complimentary entr�e with the purchase of an entr�e."

But if string is defined directly in the index.html, then it is not a problem.

Can anyone suggest a solution on how to keep the text in the separate .js file?

+2  A: 

Escape the non-Latin character to an HTML entity, &eacute; or change all aspects your development system and code editors to parse all text characters as UTF8.

UTF-8 end-to-end is the best solution.
Diodeus
If you have to encode to go down an ASCII-only JSON pipe, you should use string literal escaping: `entr\u00E9e`. HTML-encoding isn't related to JSON and you generally don't want to be displaying text text strings as raw HTML (leads to XSS problems).
bobince
+1  A: 

Make sure that the included file is saved using the same encoding as the page.

If for example the page is saved as UTF-8, the included file will also be loaded as UTF-8.

Guffa
Thanks, json.js is now re-saved using UTF-8 encoding by notepad and the problem is solved.
Jazure