HTML-escape javascript strings
12
Similar to Perl's CGI::escapeHTML(), though (because we can!) this adds it as a method to all String objects.
String.prototype.escapeHTML = function () {
return(
this.replace(/&/g,'&').
replace(/>/g,'>').
replace(/</g,'<').
replace(/"/g,'"')
);
};
// example
document.getElementById('some_div').innerHTML =
document.getElementById('some_textarea').value.escapeHTML()






There are currently no comments for this snippet.