<p>Here's a very simple Prototype example.</p>
<p>All it does is, on window load, an ajax call which sticks some html into a div.</p>
<pre class="lang-html prettyprint-override"><code>&lt;html&gt;
    &lt;head&gt;
        &lt;script type=&quot;text/javascript&quot; src=&quot;scriptaculous/lib/prototype.js&quot;&gt;&lt;/script&gt;
        &lt;script type=&quot;text/javascript&quot;&gt;
            Event.observe(window, 'load', function(){
                new Ajax.Request('get-table.php', {
                    method:  'get',
                    onSuccess:  function(response){
                        $('content').innerHTML = response.responseText;
                        //At this call, the div has HTML in it
                        click1();
                    },
                    onFailure:  function(){
                        alert('Fail!');
                    }
                });
                //At this call, the div is empty
                click1();
            });

            function click1(){if($('content').innerHTML){alert('Found content');}else{alert('Empty div');}}
        &lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;&lt;div id=&quot;content&quot;&gt;&lt;/div&gt;&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p>The thing that's confusing is the context in which Prototype understands that the div actually has stuff in it.</p>
<p>If you look at the onSuccess part of the ajax call, you'll see that at that point $('content').innerHTML has stuff in it.</p>
<p>However when I check $('content').innerHTML right after the ajax call, it appears to be empty.</p>
<p>This has to be some fundamental misunderstanding on my part.  Anyone care to explain it to me?</p>
<hr />
<p><strong>Edit</strong><br />
I just want to clarify something.  I realize that the Ajax call is asynchronous.</p>
<p>Here's the actual order that things are being executed and why it's confusing to me:</p>
<ol>
<li>The page loads.</li>
<li>The Ajax request to get-table.php is made.</li>
<li>The call to click1() INSIDE onSuccess happens.  I see an alert that the div has content.</li>
<li>The call to click1() AFTER the Ajax call happens.  I see an alert that the div is empty.</li>
</ol>
<p>So it's like the code is executing in the order it's written but the DOM is not updating in the same order.</p>
<hr />
<p><strong>Edit 2</strong>
So the short answer is that putting the code in onSuccess is the correct place.</p>
<p>Another case to consider is the one where you do an Ajax call and then do another Ajax call from the onSuccess of the first call like this:</p>
<pre class="lang-js prettyprint-override"><code>new Ajax.Request('foo.php',{
  method:  'get',
  onSuccess:  function(response){
    doAnotherAjaxCall();
  }
});

function doAnotherAjaxCall(){
  new Ajax.Request('foo.php',{
    method:  'get',
    onSuccess:  function(response){
      //Anything that needs to happen AFTER the call to doAnotherAjaxCall() above
      //needs to happen here!
    }
  });
}
</code></pre>

[{'answer_id': 75228, 'author': 'Jan Krüger', 'author_id': 12471, 'author_profile': 'https://Stackoverflow.com/users/12471', 'pm_score': 4, 'selected': True, 'text': '<p>The first letter of AJAX stands for "asynchronous". This means that the AJAX call is performed in the background, i.e. the AJAX request call <em>immediately returns</em>. This means that the code immediately after it is normally actually executed <em>before</em> the onSuccess handler gets called (and before the AJAX request has even finished).</p>\n\n<p>Taking into account your edited question: in some browsers (e.g. Firefox), alert boxes are not as modal as you might think. Asynchronous code may pop up an alert box even if another one is already open. In that case, the newer alert box (the one from the asynchronous code) gets displayed on top of the older one. This creates the illusion that the asynchronous code got executed first.</p>\n'}, {'answer_id': 75240, 'author': 'Lasar', 'author_id': 9438, 'author_profile': 'https://Stackoverflow.com/users/9438', 'pm_score': 0, 'selected': False, 'text': '<p>Without having tried your code: The AJAX call is executed asynchronously. Meaning that your Ajax.Request fires, then goes on to the click1() call that tells you the div is empty. At some point after that the Ajax request is finished and content is actually put into the div. At this point the onSuccess function is executed and you get the content you expected.</p>\n'}, {'answer_id': 75271, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>It's Ajax call, which is asynchronous. That means right after that request call, response hasn't come back yet, that's why $('content') is still empty.</p>\n"}, {'answer_id': 75486, 'author': 'Stimy', 'author_id': 8852, 'author_profile': 'https://Stackoverflow.com/users/8852', 'pm_score': 0, 'selected': False, 'text': '<p>The onSuccess element of the function call you are making to handle the AJAX call is executed at the time you receive a response from get-table.php.  This is a separate Javascript function which you are telling the browser to call when you get an answer from get-table.php.  The code below your AJAX.Request call is accessed as soon as the AJAX.Request call is made, but not necessarily before get-table.php is called.  So yes I think there is a bit of a fundamental misunderstanding of how AJAX works, likely due to using a library to hide the details.</p>\n'}]
['https://Stackoverflow.com/questions/75181', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/305/']