My htmlspecialchars() functions for Javascript
December 22, 2007
I thought I'd post my htmlspecialchars() functions for Javascript. This should mimic the PHP version of htmlspecialchars(). I also include rhtmlspecialchars() in case you need to do the reverse.
function htmlspecialchars(str) {
if (typeof(str) == "string") {
str = str.replace(/&/g, "&"); /* must do & first */
str = str.replace(/"/g, """);
str = str.replace(/'/g, "'");
str = str.replace(//g, ">");
}
return str;
}
function rhtmlspecialchars(str) {
if (typeof(str) == "string") {
str = str.replace(/>/ig, ">");
str = str.replace(/</ig, "<");
str = str.replace(/'/g, "'");
str = str.replace(/"/ig, '"');
str = str.replace(/&/ig, '&'); /* must do & last */
}
return str;
}
September 24, 2010
you rock, works great!April 22, 2011
Excellent job. Thank you.April 22, 2013
Thanks you sooooo much. Works great. :)