jones468 — 2012-10-31T17:57:58-04:00 — #1
I have this html page with charset set to "utf-8", and it is important that that charset remains in that setting.
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">
// alert('<?php echo utf8_encode("à") ?>');
The strange result of encodeURIComponent is "%EF%BF%BD".
I hope you can help me to resolve this problem, because I met that one in many occasions during programming...
aussiejohn — 2012-10-31T19:47:31-04:00 — #2
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.