Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using document.createDocumentFragment() and innerHTML to manipulate a DOM

I'm creating a document fragment as follow:

var aWholeHTMLDocument = '<!doctype html> <html><head></head><body><h1>hello world</h1></body></html>'; var frag = document.createDocumentFragment(); frag.innerHTML = aWholeHTMLDocument; 

The variable aWholeHTMLDocument contains a long string that is the entire html document of a page, and I want to insert it inside my fragment in order to generate and manipulate the DOM dynamically.

My question is, once I have added that string to frag.innerHTML, shouldn't it load this string and convert it to a DOM object?

After setting innerHTML, shouldn't I have access to the DOM through a property?

I tried frag.childNodes but it doesn't seem to contain anything, and all I want is to just access that newly created DOM.

like image 434
Loic Duros Avatar asked Nov 20 '11 14:11

Loic Duros


People also ask

What does document body innerHTML do?

Value. A string containing the HTML serialization of the element's descendants. Setting the value of innerHTML removes all of the element's descendants and replaces them with nodes constructed by parsing the HTML given in the string htmlString.

Why should you create DOM elements instead of using innerHTML?

#1) createElement is more performant Therefore, it is less efficient than creating a new element and appending to the div. In other words, creating a new element and appending it to the DOM tree provides better performance than the innerHTML .

What is createDocumentFragment?

createDocumentFragment() Creates a new empty DocumentFragment into which DOM nodes can be added to build an offscreen DOM tree.

Why you shouldn't use innerHTML?

The use of innerHTML creates a potential security risk for your website. Malicious users can use cross-site scripting (XSS) to add malicious client-side scripts that steal private user information stored in session cookies. You can read the MDN documentation on innerHTML .


2 Answers

While DocumentFragment does not support innerHTML, <template> does.

The content property of a <template> element is a DocumentFragment so it behaves the same way. For example, you can do:

var tpl = document.createElement('template'); tpl.innerHTML = '<tr><td>Hello</td><td>world</td></tr>'; document.querySelector('table').appendChild(tpl.content); 

The above example is important because you could not do this with innerHTML and e.g. a <div>, because a <div> does not allow <tr> elements as children.


NOTE: A DocumentFragment will still strip the <head> and <body> tags, so it won't do what you want either. You really need to create a whole new Document.

like image 106
chowey Avatar answered Oct 14 '22 18:10

chowey


You can't set the innerHTML of a document fragment like you would do with a normal node, that's the problem. Adding a standard div and setting the innerHTML of that is the common solution.

like image 32
Johan Avatar answered Oct 14 '22 17:10

Johan