Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery DOM manipulation efficiency - building entire page with JavaScript
    primarykey
    data
    text
    <p>I'm going to be starting off with a completely blank page (no elements other than html, head and body) and will then build the page using jQuery. The page contents will be in the form of JSON from an AJAX request. The contents from JSON will not have any HTML. The HTML with content will be built for different sections of the page depending on the structure of the JSON object. </p> <p>This page will have various sliders, modals and other "dynamic" content.</p> <p>My question is, will it be faster (lets take IE7 as the lowest common denominator) to build the HTML as one large string (using a string builder which is much faster than standard concatenation) and inject this into the body in a bulk fashion, i.e.</p> <pre><code>var html = "&lt;div id='content'&gt;&lt;p&gt;All markup required for the page, built from the contents of the JSON object&lt;/p&gt;&lt;/div&gt;&lt;div id='slider'&gt;...&lt;/div&gt;...etc." $("body").html(html) </code></pre> <p>And then when that is in the DOM, use jQuery to find and apply plugins to the various dynamic parts, i.e.</p> <pre><code>$("#slider").sliderPlugin(options); </code></pre> <p><strong>OR</strong></p> <p>Would it be better to create each element (or some) as a variable, then append to the body? i.e.</p> <pre><code>var content = $('&lt;div/&gt;', {id: "content"}) var slider = $('&lt;div/&gt;', {id: "slider", html="&lt;ul&gt;&lt;li&gt;...&lt;/li&gt;&lt;/ul&gt;"}).appendTo(content); $('body').append(content) </code></pre> <p>then with this approach I don't have to query the DOM, I only have to do this:</p> <pre><code>slider.sliderPlugin(options); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload