escaping html markup
I'm building a html table using javascript on the fly and I want to add a cell that may contain html markup. I do not want that ma开发者_运维知识库rkup interpreted but simply displayed.
LogHTML += "<tr><td>" + data + "</td></tr>";
How can I do that? Is there some function or html markup that will accomplish that?
I tried pre but then I get extra spaces.
LogHTML += "<tr><td>"+data.replace(/</g,"<")+"</td></tr>";
That's all you need.
The best solution is to use DOM instead of innerHTML
and to create text node for data
.
But you can try this as a quick solution or
function htmlentities(s){
var div = document.createElement('div');
var text = document.createTextNode(s);
div.appendChild(text);
return div.innerHTML;
}
精彩评论