tags:

views:

142

answers:

4

Hi all

In Javascript I'm clearing out the contents of a DIV and then re-populating it, is there any way to suspend layout of these elements or lock the UI until I've finished slashing away at the HTML? I don't want any messages popping up, I just don't want to see the flicker as the items are removed/added.

Thanks Kev

+5  A: 

You could build the new element’s content in a DocumentFragment before inserting it into the actual document:

var fragment = document.createDocumentFragment();
// build node in fragment

var div = /* … */; // DIV that should be replaced
div.parentNode.replaceChild(fragment, div);
Gumbo
most graphics libraries use the same 'trick', called double buffering
xtofl
A: 

hide it by adding the style attribute to display:none, do the stuff, then redisplay the div after

Mark Schultheiss
This then causes the div to collapse, I'm going to check the height settings and see if I can maintain a consistent height when doing this, thanks.
Mantorok
You could put an outer div in place to hold the size consistent...just a thought.
Mark Schultheiss
@Mark, this is what I did in the end, I forgot that the div place-holder was being hidden, so I put a placeholder within that div and all is fine.
Mantorok
@Mantorok - you can mark this as answer (click the check mark next to it) - doing that gives reputation points but more important reclasifieds this as an answered question.
Mark Schultheiss
A: 

only advise could be to update the div content as a whole , by building the entire content html and then add it to the div innerHTML at once, as opposite to add element to the div one by one that is more computational expensive as it involves rebuild the dom tree each time (that easily also cause flickering)

luca
Hi, I am adding it all at once using JQuery's html() function.
Mantorok
A: 

Hi all

I found that by applying a fixed height to the div sorted it, the height wasn't set before so it was growing/shrinking with the content.

Thanks for the answers, oh and Doh!

Kev

Mantorok