Created
October 8, 2013 15:36
-
-
Save joshbeckman/6886647 to your computer and use it in GitHub Desktop.
Leverage the browser to escape or unescape HTML via javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function escapeHtml(str) { | |
var div = document.createElement('div'); | |
div.appendChild(document.createTextNode(str)); | |
return div.innerHTML; | |
}; | |
function unescapeHtml(escapedStr) { | |
var div = document.createElement('div'); | |
div.innerHTML = escapedStr; | |
var child = div.childNodes[0]; | |
return child ? child.nodeValue : ''; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment