Can an Ajax call complete before the DOM is loaded?

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.

Answers:

Answer

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.

Answer

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.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.