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.

4
  • "French encoded characters" are HTML encoded French characters, I believe? It seems like this is a question about decoding arbitrary HTML-encoded strings. Commented Jul 14, 2011 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. Commented Jul 14, 2011 at 20:51
  • unescapeHTML and escapeHTML definitely only converts <, > and &. My question is how are you outputting with javascript that doesn't already decode? Commented Jul 14, 2011 at 21:07
  • A string is loaded into a js variable server side and the client side shows var somevar = "Mettre &agrave; jour" for example. Then if you alert that or use that anywhere, it stays like that. In the browser, the output ends up being &amp;agrave; Commented Jul 14, 2011 at 21:12

3 Answers 3

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/

Sign up to request clarification or add additional context in comments.

2 Comments

Yep, prototypes unescapeHTML doesn't work on those french html characters: jsfiddle.net/nwellcome/MRqnQ
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.
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.

2 Comments

Sorry the first option isn't what I am looking for, but the second looks like my only solution at this point.
Yes, the first option was just to show how the two functions go together. decodeURI is what I was suggesting.
0

Try using that :

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.