I have Javascript in an XHTML web page that is passing UTF-8 encoded strings. It needs to continue to pass the UTF-8 version, as well as decode it. How is it possible to decode a UTF-8 string for display?
<script type="text/javascript">
// <![CDATA[
function updateUser(usernameSent){
var usernameReceived = usernameSent; // Current value: Größe
var usernameDecoded = usernameReceived; // Decode to: Größe
var html2id = '';
html2id += 'Encoded: ' + usernameReceived + '<br />Decoded: ' + usernameDecoded;
document.getElementById('userId').innerHTML = html2id;
}
// ]]>
</script>
Größe
? It's not URL encoded. – Hamletmeta
tag like<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />
and XML declaration like<?xml version="1.0" encoding="UTF-8"?>
. – Uncrowned<head>
section. Send BOM to client also do the job. – HamletencodeURIComponent()
anddecodeURIComponent()
will assume the data is UTF-8 encoding. – HamletGröÃe
on the web page not decoded. – Penisutf8_encode
. Do you need it? Do you know why you need it? – Depolarizeutf8_encode
. Not necessarily.utf8_encode
transforms the encoding of a string from ISO 8859-1 to UTF-8. It tries to do that even if the string is already UTF-8. UTF-8 "Größe" →utf8_encode
→ "GröÃe" →utf8_encode
"GröÃÂe". If you apply it when you don't need it, your string screws up. – Depolarize