This page is Almost Ready

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

createDocumentFragment

Summary

Creates a new document fragment (DocumentFragment) object.

Method of dom/Documentdom/Document

Syntax

var documentFragment = document.createDocumentFragment();

Return Value

Returns an object of type DOM NodeDOM Node

The created DocumentFragment instance.

Examples


var div, docFrag = document.createDocumentFragment(),  i = 0, thismany = 1000;

while ( i < thismany) {
    // Creates a new <div> element if one doesn't exist. Clones it if one does.
    (div === undefined) ? document.createElement('div') : div.cloneNode(false);

    // Appends div to the document fragment.
    docFrag.appendChild(div);

    i++;
}

// Appends the fragment and child nodes to the document body.  Makes one DOM update instead of 1000
document.body.appendChild(docFrag);

Related specifications

DOM Level 3 Core
Recommendation

Attributions