Escaping Contents of HTML with jQuery -
i'm running function cycle through elements on email newsletter , escape html.
function escapechars() { iframe.contents().find('[data-mailbuilder="article-content"]') .children() .each(function() { $(this).text(escapehtml($(this).text())); }); } function escapehtml(unsafe) { return unsafe .replace(/&/g, "&") .replace(/</g, "<") .replace(/>/g, ">") .replace(/"/g, """) .replace(/'/g, "'"); }
however jquery seems replace '&' '&' without escaping character.
any ideas?
to render html use .html()
instead of .text()
. so, change to:
$(this).html(escapehtml($(this).text()));
Comments
Post a Comment