I have a bit of an odd situation:<ol><li>Main HTML page is served in UTF-16 character set (due to some requirements out-of-scope for this question)</li> <li>HTML page uses
<script>tags to load external scripts (i.e. they have
When loading the page described above, both Firefox and Chrome (current versions) throw errors saying that the first character of the script files are invalid.
Looking at the "Network" tabs of the respective dev-tools views shows the files are just fine (they render in the previewer just fine).
My conclusion was that the browsers are becoming confused as to what the encoding should be for "the whole page" or some similar foolishness.
So I tried adding a
charsrt="UTF-8" attribute to the
<script> tags and that seems to solve the problem.
But I <em>really</em> shouldn't have to do that, should I?
First of all, the server is telling the client what the document's type is. It's
charset is only applicable to
text/* MIME-types). Okay, I can understand why there might be some ambiguity, there.
Lastly, the enclosing page shouldn't have to know what the encoding of its dependencies are. In fact, it might be <em>impossible</em> for it to know, and explicitly-specifying the
charset then tightly-couples the page to its dependencies and vice-versa.
Is there a way to get the browser to correctly-detect the character set of these dependencies without specifying the
charset in the page itself?
Without a BOM in the file, or an explicit
charset in the
Content-Type for the file, the encoding of the file is ambiguous. The browser <em>might</em> assume UTF-8 (and should, per <a href="https://tools.ietf.org/html/rfc4329" rel="nofollow">RFC 4329</a>), but if the script contains any non-ASCII characters that are not actually encoded in UTF-8, the file won't process properly.
However, HTML 5 Section 4.11 dictates that a
<script>'s fallback encoding is the document's encoding if the
<script> does not have a
charset attribute. The fallback takes effect if there is no BOM or
charset to specify the file's actual encoding.
So, either make sure your HTML and JS files are always using the same encoding, or else you have to be explicit about the JS file's
charset, one way or the other.