0

I am looking for a prototype.js or other js function to decode html encoded entities. I am using 1.6.1 of Prototype.js and unescapeHTML does not work on French encoded characters. I believe from what I read, that is only works on a few select entities.

Can someone point me in the right direction on how I would do something like this with javascript? I would normally be able to use the .text() with jQuery, but right now the main library used is Prototype.

Thanks.

fanfavorite
  • 5,128
  • 1
  • 31
  • 58
  • "French encoded characters" are HTML encoded French characters, I believe? It seems like this is a question about decoding arbitrary HTML-encoded strings. – Wylie Jul 14 '11 at 20:46
  • Yeah, trying to get the right lingo here. Basically encoded HTML needs to be unencoded. The reason is the javascript outputs the html rather than the final output that a browser displays. – fanfavorite Jul 14 '11 at 20:51
  • `unescapeHTML` and `escapeHTML` definitely only converts `<`, `>` and `&`. My question is how are you outputting with javascript that doesn't already decode? – clockworkgeek Jul 14 '11 at 21:07
  • A string is loaded into a js variable server side and the client side shows var somevar = "Mettre à jour" for example. Then if you alert that or use that anywhere, it stays like that. In the browser, the output ends up being &agrave; – fanfavorite Jul 14 '11 at 21:12

3 Answers3

3

How about this:

function decode(str) {
    var div = document.createElement('div');
    div.innerHTML = str;
    return div.innerHTML;
}

Doesn't return &amp; properly but works fine for french ones. Updated fiddle:http://jsfiddle.net/mrchief/MRqnQ/3/

Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
Mrchief
  • 75,126
  • 20
  • 142
  • 189
  • Yep, prototypes unescapeHTML doesn't work on those french html characters: http://jsfiddle.net/nwellcome/MRqnQ/ – nwellcome Jul 14 '11 at 20:54
  • Thanks. The problem is I can't change the output at this time as the strings are everywhere, I just can change the value of the string itself, so outputting the innerHTML isn't an option I don't think. – fanfavorite Jul 14 '11 at 21:04
0

The built-in decodeURI function may be what you're looking for. It ignores "special" characters, but will turn an arbitrary URI-encoded string into what it represents.

Example:

encodeURI("Déjà vu") = "D%C3%A9j%C3%A0%20vu"

decodeURI("D%C3%A9j%C3%A0%20vu") = "Déjà vu"

An alternative may be to use a regular expression.

Community
  • 1
  • 1
Wylie
  • 1,054
  • 2
  • 12
  • 20
  • Sorry the first option isn't what I am looking for, but the second looks like my only solution at this point. – fanfavorite Jul 14 '11 at 21:13
  • Yes, the first option was just to show how the two functions go together. `decodeURI` is what I was suggesting. – Wylie Jul 14 '11 at 21:26
0

Try using that :

http://phpjs.org/functions/htmlentities:425

ChristopheCVB
  • 7,269
  • 1
  • 29
  • 54