views:

43

answers:

2

I am grabbing data through a jQuery Ajax call, and displaying it on the page. I need to wait for both the DOM to load and for the Ajax call to complete before I can use the data to display it on the page. Can an Ajax call ever complete before the DOM has loaded? I'm just trying to determine where I need to put my method that will manipulate the DOM and use the data I'm getting back.

+1  A: 

I would wait until the DOM has loaded before making your AJAX call. While it is unlikely that the DOM READY will take longer than the AJAX call, it's just good practice.

Diodeus
I suppose that is the best path to go down. I was thinking that I could get the Ajax call started before the DOM had completely loaded for the sake of saving some time (although the time saved would probably be a small fraction).
Ek0nomik
A: 

You could call and receive an AJAX call before the page loaded to save a tiny bit of time. I would suggest making the script call after you know your element is loaded in the page.

<div id="loadFile"></div>
<script>
    $("#loadFile").load('filename');
</script>

The time saved is probably negligible, though. If you're making an AJAX call before the DOM is loaded, why use AJAX at all? You should try to write some server side code to do the same thing.

MillsJROSS