Escaping HTML

If you want to escape HTML symbols in JavaScript code you may think you should use escape() function, but not so fast. You can use this escape() for URL’s, but if you want to render HTML use the function below :

function escapeHTML(str){ return new Option(str).innerHTML }

-----

> escape("<div>")
'%3Cdiv%3E'

> escapeHTML("<div>")
'&lt;div&gt;'