Created
June 15, 2011 07:43
-
-
Save davidcalhoun/1026657 to your computer and use it in GitHub Desktop.
Shortest HTMLEntities in JavaScript ever
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
/* From Ben McMahan: | |
http://www.quora.com/What-are-the-most-interesting-HTML-JS-DOM-CSS-hacks-that-most-web-developers-dont-know-about/answer/Ben-McMahan | |
Example usage: | |
HTMLEntities('<div id="foo">bar</div>'); // '<div id="foo">bar</div>' | |
*/ | |
function HTMLEntities(a) { | |
var b = document.createElement('a'); | |
b.innerText = a; | |
return b.innerHTML; | |
} | |
/* And a decoder inspired by the above code | |
Usage: HTMLEntitiesDecode('<div id="foo">bar</div>'); // '<div id="foo">bar</div>' | |
*/ | |
function HTMLEntitiesDecode(a) { | |
var b = document.createElement('a'); | |
b.innerHTML = a; | |
return b.innerText; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment