开发者

How do I convert character encodings with Javascript? JQuery

Ive got this in an XML file that i parse with JQuery.

<title>L&#229;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(); 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜