2

Typically, I use utf-8 for everything. Is utf-8 acceptable for JavaScript

header('Content-Type: application/javascript; charset=utf-8');

Reason I ask is http://kunststube.net/encoding/ states:

What does it mean for a language to support Unicode then? Javascript for example supports Unicode. In fact, any string in Javascript is UTF-16 encoded. In fact, it's the only thing Javascript deals with. You cannot have a string in Javascript that is not UTF-16 encoded. Javascript worships Unicode to the extent that there's no facility to deal with any other encoding in the core language. Since Javascript is most often run in a browser that's not a problem, since the browser can handle the mundane logistics of encoding and decoding input and output.

Should I be using utf-16 instead of utf-8? Or does this article pertain to how JavaScript encodes text, not how the browser deencodes the text of a file which happens to be JavaScript?

user1032531
  • 24,767
  • 68
  • 217
  • 387

2 Answers2

1

Or does this article pertain to how JavaScript encodes text, not how the browser deencodes the text of a file which happens to be JavaScript?

Correct. JavaScript String values are stored and processed as a sequence of UTF-16 code units but this is unrelated to how JS source code is loaded.

UTF-16 is a poor choice for JS source encoding because it is not ASCII-compatible. There are a number of browser quirks that break non-ASCII charsets like UTF-16, so it's generally best avoided as a file encoding on the web.

header('Content-Type: application/javascript; charset=utf-8');

This is fine but note that not all browsers will necessarily respect the charset here; some (especially older) browsers will decode the script file using the same encoding as was used on the page that linked to the script. If that page is served as UTF-8 too, no problems.

bobince
  • 528,062
  • 107
  • 651
  • 834
  • Thanks bobince, I suspected so much, but wasn't sure. I am really learning about encoding for the first time, and didn'tn want to take anything for granted. – user1032531 Jan 12 '15 at 12:01
0

Search the Javascript standard http://www.ecma-international.org/ecma-262/5.1/

Enable UTF-8 encoding for JavaScript

What you are seeing however is that utf-8 is very broadly supported and the right thing to be using for resources served over the web especially.

Yes you 'can' encode it with utf-16, ASCII, etc, but must be supported by client programs.

Community
  • 1
  • 1
Ted Johnson
  • 4,315
  • 3
  • 29
  • 31
  • Just found this nice summary that seems relevant: http://stackoverflow.com/questions/9254891/what-does-content-type-application-json-charset-utf-8-really-mean – Ted Johnson Jan 11 '15 at 18:59
  • Thanks Ted, I think you are right about using utf-8 JavaScript, but not positive. The link in your comment states JSON is definitely utf-8. The spec you referenced describes both utf-8 and utf-16. The other link just states to be sure to encode files using something that can handle all the characters. – user1032531 Jan 11 '15 at 19:11