How to parse or escape html in javascript??/
Sometimes we want to create html using JavaScript.
like on this page
<div > this is </div>
or may be
<b> hello </b>
for this use
http://jsperf.com/encode-html-entities
and resource is
http://stackoverflow.com/questions/5499078/fastest-method-to-escape-html-tags-as-html-entities
There are more solutions for this
and finally my favourite one is.
like on this page
<div > this is </div>
or may be
<b> hello </b>
for this use
var tagsToReplace = {
'&': '&',
'<': '<',
'>': '>'
};
function replaceTag(tag) {
return tagsToReplace[tag] || tag;
}
function safe_tags_replace(str) {
return str.replace(/[&<>]/g, replaceTag);
}
link for fiddle ishttp://jsperf.com/encode-html-entities
and resource is
http://stackoverflow.com/questions/5499078/fastest-method-to-escape-html-tags-as-html-entities
There are more solutions for this
and finally my favourite one is.
function escapeHTML( string )
{
var pre = document.createElement('pre');
var text = document.createTextNode( string );
pre.appendChild(text);
return pre.innerHTML;
}