Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-12-2010, 03:14 PM   PM User | #1
ellisd5
Regular Coder

 
ellisd5's Avatar
 
Join Date: Jun 2002
Location: Uk
Posts: 160
Thanks: 5
Thanked 0 Times in 0 Posts
ellisd5 is an unknown quantity at this point
Internet Explorer DOM Modification speed

Hi all,

I'm justing wondering about the behavior of JS in regards to adding elements, suppose I have something like this

Code:
<div id="myDiv"></div>
...
function test() {
    for (i=0 i<100; i++) {
        var obj = document.create("div");
        // do stuff to style div and set content
        document.getElementById("myDiv").appendChild(obj);
    }
    // DO SOMETHING WITH ONE OF THESE DIVS
}
I'm just wondering at the point I hit that "// DO SOMETHING WITH ONE OF THESE DIVS", are all the divs I have added in the DOM available to access?

I ask because I have some code at work in which a tester is reporting an error that happens which I can't reproduce, and they and others have had it a few times.

The only way I can explain it in my mind is if the div is not available to me at the time of execution. So I'm just looking to rule it out or confirm my hunch.

Is document.getElementById("myDiv").appendChild(obj); synchronous and the next line of code wont execute until the DOM is ready or is it in fact a asynchronous call and therefore adding alot of elements to the DOM could result in a lag so some divs or not available straight away.

Any information much appreciated, I hope I have been clear enough. I'm using IE7 and so are the testers.

Thanks,
Dale
__________________
Dale Ellis
__________________
ellisd5 is offline   Reply With Quote
Old 11-12-2010, 10:17 PM   PM User | #2
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,554
Thanks: 9
Thanked 480 Times in 463 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
appending should stall the loop.

you can use cloneNode(true) to create, which is a lot faster.
also, cache the div instead of calling document.getElementById("myDiv") each time.

Code:
myDiv=document.getElementById("myDiv");
//...
myDiv.appendChild(obj);
those two changes will vastly speed the loop execution.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:15.2% IE7:0.5% IE8:8.4% IE9:8.5% IE10:8.5%
rnd me is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:06 AM.


Advertisement
Log in to turn off these ads.