Skip to content

Javascript source file encodings

Alexander Shtuchkin edited this page Apr 22, 2017 · 2 revisions

Context

Node.js and most other javascript environments always load source .js files using 'utf-8' encoding. This fact is heavily used by iconv-lite library to save space and make encoding table load faster.

Problem

Browsers by default use 'latin1' encoding when they load javascript files, so if you compiled iconv-lite for browser (e.g. using Browserify), it might not work. See #142 for an example. Fortunately, iconv-lite now detects this condition and prints out a warning with link to this page.

Solution

There are several ways you can ensure that javascript files are treated as utf-8 files in browser:

  • add <meta charset="utf-8"> to the html file.
  • use <script src="your-file.js" charset="utf-8"></script> to load the script.
  • add Content-type: application/javascript; charset=utf-8 header to the http request of js file.
Clone this wiki locally