views:

65

answers:

3

Hi. I am designing a site that uses progressive enhancement and has also has a mobile version. I am wanting to use jquery to add a slideshow to the desktop site and want to know the best method for adding a large quantity of HTML. A sample of what I intend to add is:

<!-- Slideshow -->
<div class="scrollable">
<!-- "previous page" action -->
<a class="prev browse left"></a>   

<!-- root element for the items -->
<ul>
    <li>
        <img src="images/slideshow/image1.jpg" alt="image 1" title="image 1" />
    </li>

    <li>
        <img src="images/slideshow/image2.jpg" alt="image 2" title="image 2" />
    </li>

    <li>
        <img src="images/slideshow/image3.jpg" alt="image 3" title="image 3" />
    </li>

    <li>
        <img src="images/slideshow/image4.jpg" alt="image 4" title="image 4" />
    </li>

    <li>
        <img src="images/slideshow/image5.jpg" alt="image 5" title="image 5" />
    </li>
</ul>

<!-- "next page" action -->
<a class="next browse right"></a>

Would I be better using the append or HTML method for something like this. I also had another idea that I might create a variable with the HTML inside and use JQuery to add the contents but I have tried and failed on that one (lack of sufficient knowledge!). I would appreciate any advice.

+1  A: 

Using append is fine, but I would look to use it just once, or as little as you can, and construct the HTML in advance, holding it in a string variable.

In general, very little DOM manipulation should really be undertaken. You should be hitting the DOM in this way only when is absolutely necessary. Let us examine the following example:

var $myList = $("#myList");   

for (i=0; i<1000; i++){
    $myList.append("This is list item " + i);
}

This code adds 1000 lines to an HTML list. This is done with 1000 successive calls to the .append() method, and hence, 1000 manipulations to the DOM. The following code, modified from the example above demonstrates how this can be made more efficient:

var $myList = $("#myList");
var li = "";   

for (i=0; i<1000; i++){
    li += "<li>This is list item " + i + "</li>";
}  

$myList.append(li);

See the following article: http://www.jameswiseman.com/blog/2010/04/20/jquery-standards-2-dom-manipulation/

James Wiseman
It's not necessarily 'more efficient' to use string concatenation. It depends on the kind of markup structure (how deep, how big) which method is faster.
jAndy
@James thanks for this I appreciate the help. For this site I will have a desktop stylesheet + JS file and a separate mobile stylesheet + JS file so do you think it would be more efficient to just hide the slideshow with css for the mobile version. Would the HTML code take up any more bandwidth if hidden with css?
mtwallet
+1  A: 

You can do benchmarks for any kind of comparisons, like this:

function test( name, fn, n, next ) {

  var n = n || 100; // default number of runs
  var start, end, elapsed;

  setTimeout(function() { 
    start = Number(new Date());   
    for ( ; n--; ) {
      fn() 
    }
    end = Number(new Date());

    elapsed = end - start;

    // LOG THE RESULT
    // can be: $("#debug").html(name + ": " +  elapsed + " ms");
    console.log(name + ": " +  elapsed + " ms")); 

    next && next();
  }, 0);
}

test("append", function() {
  $("#elem").append( LARGE HTML );
});

test("html", function() {
  $("#elem").html( LARGE HTML );
});

What I would try is create a new element and set it's html to the big chunk, and then append this element to the dom. It will be faster in my opinion, because when applying the html the element is not visible so the browser can work faster, and then it needs to append only one element to the DOM.

See it in action. (click preview)

(it seems that html() is faster)

galambalazs
@galambalazs thanks for this I appreciate the help. Could you explain what is going on in the first part of your code?
mtwallet
it's a simple benchmarking code. You can test the speed of various things to compare them. But it doesn't really matter. You don't need to modify it. Just use the *test function* to measure differences of the two or possibly other methods of inserting HTML.
galambalazs
it will log the results to the console. Be sure to use Firebug, or Chrome's built in console.
galambalazs
or add an element with the id #debug, and use it to store the results. See my update.
galambalazs
@galambalaz thanks for your help.
mtwallet
A: 

I would use the native innerHTML for the entire chunk, this is proven to be the fastest way to do DOM manipulations. See f.ex here: http://www.quirksmode.org/dom/innerhtml.html

<div id="container"></div>

<script>
var html = '<p>Some HTML</p>';
document.getElementById('container').innerHTML = html;
</script>
David
innerHTML is good if you know what you're doing, but there are many x-browser gotchas (tables, forms, etc...)
galambalazs