Monday, May 6, 2024
 Popular · Latest · Hot · Upcoming
188
rated 0 times [  192] [ 4]  / answers: 1 / hits: 38492  / 15 Years ago, sat, june 20, 2009, 12:00:00

can anyone help? I have small procedure to read in an UTF-8 file with javascript using XMLHttpRequest.. this file has european characters like miércoles sábado etc.. Notice the accents..



But when being read in .. the characters are all messed up.. I have checked the file and it is perfect.. it must be the procedure for reading in..



heres an example i have file that contains, the file is perfect, it happens to be javascript but it doesn't matter.. any UTF-8 encoding file with special characters gives me the same issue



this.weekDays = new Array(Lunes, Martes, Miércoles, Jueves, Viernes, Sábado, Domingo);



but when returned and read by the procedure below it is like this (notice the funny characters in sabado and miercoles)



this.weekDays = new Array(Lunes, Martes, Miércoles, Jueves, Viernes, Sábado, Domingo);



Here is my procedure - its very small...



var contentType = application/x-www-form-urlencoded; charset=utf-8;

var request = new XMLHttpRequest();
request.open(GET, path, false);
request.setRequestHeader('Content-type', contentType)

if (request.overrideMimeType) request.overrideMimeType(contentType);

try { request.send(null); }
catch (e) { return null; }
if (request.status == 500 || request.status == 404 || request.status == 2 || (request.status == 0 && request.responseText == '')) return null;

//PROBLEM HERE is with european charcters that are read in

print(request.responseText);


return request.responseText;

More From » encoding

 Answers
95

EDIT: Seems that this answer, although accepted, is suboptimal, so for anyone coming here with a similar problem, check out Ricardo's answer


I think you have to use a different way to print the characters, for example, see the code at the end of this discussion:


<script>
function getUnicode(num) {
num = num.toString(16);
if (num.length < 3) {
for ( var i = num.length; i < 4; i++) {
num = '0' + num;
}
}
return ( "&#" + num + ";" );
}

for ( var i = 0; i < 65355; i++) {
document.write(getUnicode(i));
}
</script>

[#99275] Tuesday, June 16, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bryonk

Total Points: 161
Total Questions: 116
Total Answers: 107

Location: Albania
Member since Sun, Nov 22, 2020
4 Years ago
bryonk questions
;