Posts Tagged UnscapeHTML
Unscape HTML in JavaScript
Posted by admin in JavaScript on July 13th, 2009
This function strips tags and converts the entity forms of special HTML characters to their normal form. I created this function with the help of code of Prototype JavaScript library.
<script language="javascript"> function unscapeHTML(str) { var div = document.createElement('div'); div.innerHTML = str.replace(/<\/?[^>]+>/gi, ''); if (div.childNodes[0]) { if (div.childNodes.length > 1) { var retStr; for (var i = 0; i < div.childNodes.length; i++) { retStr += div.childNodes[i].nodeValue; } return retStr; } else { return div.childNodes[0].nodeValue; } } else { return ''; } } </script>
How you can use it.
<script language="javascript"> var1 = 'x > 10'; alert(unscapeHTML(var1)); // This will alert 'x > 10' var2 = '<h1>Pride & Prejudice</h1>'; alert(unscapeHTML(var2)); // This will alert 'Pride & Prejudice' </script>