Encode accented letters in utf-8 charset


I have this html page with charset set to “utf-8”, and it is important that that charset remains in that setting.
How can I obtain a right encode using javascript even if the charset isn’t iso-8859-1?
Using PHP seems that the problem it can be resolved with utf8_encode function, but in js I have tried many things without success. :mad:
Now I post here a simple code to explain better the matter. Can you help me please?

(If you not able to see the “à” charachter within the code, it is an “a grave”);

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
   <meta http-equiv="content-type" content="text/html; charset=utf-8">
	<script type="text/javascript">
		// alert('<?php echo utf8_encode("à") ?>');

The strange result of encodeURIComponent is “%EF%BF%BD”. :confused:
I hope you can help me to resolve this problem, because I met that one in many occasions during programming…

many thanks!

When you’re working with files that you think are in UTF-8 it’s worth checking that whatever editor you used is actually using UTF-8 to save the files.

When you use encodeURIComponent it will encode the character in to a safe representation for use in URLs, if you are then using decodeURIComponent on a page with the same encoding as where the character was encoded in the first place, it should decode back in to the correct character. If however, you decode it on a page with a different encoding, you might not get the correct character back.

For example, if you had this code on the same (UTF-8 encoded) page:

console.log(encodeURIComponent("à")); // "%C3%A0"
console.log(decodeURIComponent("%C3%A0")); // "à"

You should see the correct output from these functions. If however you were to change the encoding, results might be different.

Hope this helps.