JavaScript: how to unescape HTML entities
I was searching the web for JavaScript HTML entity unescaping code. I found lots of really bad ideas. Sound familiar? Some were quite complicated.
It looked like the scriptaculous guys had the right idea. However, all I could find was a cached page with no code. A quick peek inside prototype set me on the right track.
String.prototype.unescapeHtml = function () {
var temp = document.createElement("div");
temp.innerHTML = this;
var result = temp.childNodes[0].nodeValue;
temp.removeChild(temp.firstChild)
return result;
}
var temp = document.createElement("div");
temp.innerHTML = this;
var result = temp.childNodes[0].nodeValue;
temp.removeChild(temp.firstChild)
return result;
}
And the code in action:
var hello = "Hello José";
alert(hello.unescapeHtml());
alert(hello.unescapeHtml());
October 7th, 2008 at 9:41 am
Thanks, Saved my day
November 20th, 2008 at 4:47 am
Very nice, thanks for posting it. It helped to solve my problem.
December 11th, 2008 at 3:31 am
very good solution!