Skip to content

Instantly share code, notes, and snippets.

@craiga
Created October 1, 2012 06:32
Show Gist options
  • Save craiga/3809829 to your computer and use it in GitHub Desktop.
Save craiga/3809829 to your computer and use it in GitHub Desktop.
HTML Encode for JavaScript
/**
* Based on http://stackoverflow.com/a/6020820/323826
*/
var htmlEscapes = {
'&': '&',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#x27;',
'/': '&#x2F;'
};
var htmlEscaper = /[&<>"'\/]/g;
htmlEncode = function(x) {
return ("" + x).replace(htmlEscaper, function(match) {
return htmlEscapes[match];
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment