Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining the character encoding of a JavaScript source file

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.

like image 803
Leonard Ehrenfried Avatar asked Feb 24 '10 10:02

Leonard Ehrenfried


People also ask

How do I encode a character in JavaScript?

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 encoding for JS file?

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.

How do you determine the encoding of a character?

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).

What are the 3 types of character encoding?

There are three different Unicode character encodings: UTF-8, UTF-16 and UTF-32.


2 Answers

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"/> 
like image 73
Pekka Avatar answered Sep 23 '22 20:09

Pekka


It seems that the charset attribute for the script tag is not deprecated in HTML5 (it is deprecated only for a and link tags).

like image 38
Simon Hi Avatar answered Sep 22 '22 20:09

Simon Hi