2
Through researches in the English OS, I learned to do decoding and encoding of HTML entities as follows:
var wm = (function(wm){
wm.encodeHTML = function (html) {
var t = document.createElement('textarea');
t.innerHTML = html;
return t.innerHTML;
}
wm.decodeHTML = function (html) {
var t = document.createElement('textarea');
t.innerHTML = html;
return t.value;
}
}({}));
I would like a more elegant solution (regular expression or anything else) to convert HTML entities instead of creating one textarea
and return its value.
Anyone can help?
Would something like this happen? http://stackoverflow.com/questions/1229518/javascript-regex-replace-html-chars
– Rafael Withoeft
Look "more elegant" would not be a way and
RegEx
may help a lot, but it does not suit all objectives, in your own example you used DOM (createElement
) this is totally acceptable as html entities are also part of theDOM
, and believe your code will have well acceptable ("perhaps" better than any other technique). Therefore html entity are part of the DOM, I see no harm in using their ownDOM
to convert them. Another thing, the way you made the code gets much smaller and in my opinion is the indentation combined with small and organized codes that will make elegance :)– Guilherme Nascimento
Just another detail Wallace, your function
wm.decodeHTML = function (html) {
is not closed, one is missing}
after thereturn
– Guilherme Nascimento