开发者

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 p开发者_如何学Cage. 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜