If you have a string containing HTML entities and want to unescape it, this solution (or variants thereof) is suggested multiple times:
function htmlDecode(input){
var e = document.createElement('div');
e.innerHTML = input;
return e.childNodes.length === 0 ? "" : e.childNodes[0].nodeValue;
}
htmlDecode("<img src='myimage.jpg'>");
// returns "<img src='myimage.jpg'>"
(See, for example, this answer: https://stackoverflow.com/a/1912522/1199564)
This works fine as long as the string does not contain newline and we are not running on Internet Explorer version pre 10 (tested on version 9 and 8).
If the string contains a newline, IE 8 and 9 will replace it with a space character instead of leaving it unchanged (as it is on Chrome, Safari, Firefox and IE 10).
htmlDecode("Hello\nWorld");
// returns "Hello World" on IE 8 and 9
Any suggestions for a solution that works with IE before version 10?