How do I convert character encodings with Javascript? JQuery
Ive got this in an XML file that i parse with JQuery.
<title>Lång</title>
I'm using .text() for pulling out the text, but it's wrong encoded.
How do I get it encoded to proper text? I want 'Lång' out of it.Edit:
I'm using JQuery for getti开发者_StackOverflow社区ng data. Have it on my work computer, but something like this:
$.ajax({
type: 'GET',
url: 'http://myserver/blah?query_string',
dataType: 'xml',
success: function(data) {
var blah = $(this).find("blahblah").text();
}
});
If the characters are always encoded to numbers like that, you should be able to parse out the number, convert it to and integer with parseInt(str)
then use string.fromCharCode(num)
to get the character that it represents.
You probably should just switch to utf-8. All ajax request are utf-8 by default. This might fix the problem.
Found out how to do it! HTML-encoding lost when attribute read from input field
Using this function solved all enocoding problems.
function htmlDecode(value){
return $('<div/>').html(value).text();
}
精彩评论