javascript - Using DOMParser to render html is a good way? -
i'm thinking create website chrome , firefox.
rendering html element dynamicaly, using domparser code snippet below bad way?
googled 'domparser' , didn't see codes in use case.
know best practice or general way it.
var core = {}; var somehtml = '<div class="test1"><div class="test2">this string more complicated</div></div>'; core.onready = function(){ var parser = new domparser() , parsed = parser.parsefromstring(somehtml, 'text/html') , element = parsed.body.firstchild; document.body.appendchild(element); };
there nothing wrong this. if want test if html valid before adding page.
generally, people like:
document.body.innerhtml += somehtml;
from speed point of view, you're unlikely notice massive performance hit using domparser unless you're doing lot of html.
http://jsperf.com/html-parsing-performance-element-innerhtml-vs-domparser/2
Comments
Post a Comment