PHP Ferrari
PHP Ferrari

Reputation: 15616

Enable UTF-8 encoding for JavaScript

I want to show log message in arabic language using JavaScript alert() function, for which I code:

alert('أدخل سعر الافتتاح');

which means

alert('Enter opening price');

but when i save the .js file Dreamweaver says enter image description here

and if I run the script browser says

enter image description here

this page contains

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

and i am using a lot of text in arabic which works fine.

now how can I use alert for different language?

Upvotes: 20

Views: 162962

Answers (11)

Sergio Abreu
Sergio Abreu

Reputation: 2889

I found out that sometimes the javascript engine only believes it is utf-8 in the presence of the UNICODE BOM in the js file.

Tip: Some editors like geany allows to generate BOM which are 3 bytes in the beginning of the file. They are invisible in most editors, but you can see them in vim, 3 special chars in the 0:0 position.

Just for information: the bytes value is 0xEFBBBF indicating that is a serious utf-8 file.

One workaround is to use all special chars in javscript as \xHH (hexa) format, but that's a little too geeky, you have to know the equivalents.

// Try in the console, a Brazilian word:
alert( 'Cora\xE7\xE3\x6F' );

Upvotes: 0

NewbieProgrammer
NewbieProgrammer

Reputation: 1

I found a solution to my problem that seems like yours.

For some reason a script called from a external file doesn't works with charset="UTF-8", instead i had to use charset="ISO-8859-1" into script tag.

Now I'm after the "why it works?" reason.

Upvotes: -1

user9787547
user9787547

Reputation:

For others, I just had a similar problem and had to copy all code from my file, put it to simple notepad, save it with utf-8 coding and then replace my original file.

The problem at my side was caused by using PSpad editor.

Upvotes: 2

Abdullah Dahmash
Abdullah Dahmash

Reputation: 535

I think you just need to make

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

Before calling your .js files or code

Upvotes: 3

Vladimir Starkov
Vladimir Starkov

Reputation: 19803

just add your script like this:

<script src="/js/intlTelInput.min.js" charset="utf-8"></script>

Upvotes: 52

Mauricio Palazzuoli
Mauricio Palazzuoli

Reputation: 21

Same problem here, solved with this: In Eclipse (with .js file open and on focus), go to, "File", "Properties", "Resource", "Text file encoding" choose "Other:" UTF-8, put correct characters code inside the code save your file and you are done!

Upvotes: 2

İlhan &#199;ELİK
İlhan &#199;ELİK

Reputation: 307

This is a quite old request to reply but I want to give a short answer for newcommers. I had the same problem while working on an eight-languaged site. The problem is IDE based. The solution is to use Komodo Edit as code-editor. I tried many editors until I found one which doesnt change charset-settings of my pages. Dreamweaver (or almost all of others) change all pages code-page/charset settings whenever you change it for page. When you have changes in more than one page and have changed charset of any file then clicked "Save all", all open pages (including unchanged but assumed changed by editor because of charset) are silently re-assigned the new charset and all mismatching pages are broken down. I lost months on re-translating messages again and again until I discovered that Komodo Edit keeps settings separately for each file.

Upvotes: 1

defau1t
defau1t

Reputation: 10619

I too had this issue, I would copy the whole piece of code and put in Notepad, before pasting in Notepad, make sure you save the file type as ALL files and save the doc as utf-8 format. then you can paste your code and run, It should work. ?????? obiviously means unreadable characters.

Upvotes: 0

Domenic
Domenic

Reputation: 112807

Just like any other text file, .js files have specific encodings they are saved in. This message means you are saving the .js file with a non-UTF8 encoding (probably ASCII), and so your non-ASCII characters never even make it to the disk.

That is, the problem is not at the level of HTML or <meta charset> or Content-Type headers, but instead a very basic issue of how your text file is saved to disk.

To fix this, you'll need to change the encoding that Dreamweaver saves files in. It looks like this page outlines how to do so; choose UTF8 without saving a Byte Order Mark (BOM). This Super User answer (to a somewhat-related question) even includes screenshots.

Upvotes: 26

Vitor Canova
Vitor Canova

Reputation: 3976

Try to put in the head section of your html the following:

<meta charset='utf-8'>

I think this need to be the fist in head section. More information about charset: Meta Charset

Upvotes: 7

j13r
j13r

Reputation: 2671

The encoding for the page is not set correctly. Either add a header

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

or use set the appropriate http header.

Content-Type:text/html; charset=UTF-8

Firefox also allows you to change the encoding in View -> Character encoding.

If that's ok, I think javascript should handle UTF8 just fine.

Upvotes: 1

Related Questions