I would like to print a status message to my German users, which contains umlauts (ä/ü/ö). I also would like them be in the source file rather than having to download and parse some extra file just for the messages.
However, I can't seem to find a way to define the encoding of a JS source file. Is there something like HTML's http-equiv
? Or should I define the encoding in the HTTP header?
When I simply encode the file in UTF-8 an serve it, IE displays garbage.
Javascript has provided escape() function to encode a string. But since the escape() function is now deprecated, it is better to use encodeURI() or encodeURIComponent().
What is JavaScript's default encoding for source code? UTF-8 encoding is recommended. You should save JavaScript file in UTF-8 encoding. UTF-8 is the standard encoding for much of web technologies.
One way to check this is to use the W3C Markup Validation Service. The validator usually detects the character encoding from the HTTP headers and information in the document. If the validator fails to detect the encoding, it can be selected on the validator result page via the 'Encoding' pulldown menu (example).
There are three different Unicode character encodings: UTF-8, UTF-16 and UTF-32.
Sending the encoding in the headers is always a good idea.
If that's not possible, the <script>
tag has the charset
property. W3C Reference
<script src="translations.js" type="text/javascript" charset="utf-8"/>
It seems that the charset attribute for the script tag is not deprecated in HTML5 (it is deprecated only for a and link tags).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With