Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 38063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:38:56+00:00 2026-05-10T14:38:56+00:00

Here’s a very simple Prototype example. All it does is, on window load, an

  • 0

Here’s a very simple Prototype example.

All it does is, on window load, an ajax call which sticks some html into a div.

<html>     <head>         <script type="text/javascript" src="scriptaculous/lib/prototype.js"></script>         <script type="text/javascript">             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');}}         </script>     </head>     <body><div id="content"></div></body> </html> 

The thing that’s confusing is the context in which Prototype understands that the div actually has stuff in it.

If you look at the onSuccess part of the ajax call, you’ll see that at that point $(‘content’).innerHTML has stuff in it.

However when I check $(‘content’).innerHTML right after the ajax call, it appears to be empty.

This has to be some fundamental misunderstanding on my part. Anyone care to explain it to me?


Edit
I just want to clarify something. I realize that the Ajax call is asynchronous.

Here’s the actual order that things are being executed and why it’s confusing to me:

  1. The page loads.
  2. The Ajax request to get-table.php is made.
  3. The call to click1() INSIDE onSuccess happens. I see an alert that the div has content.
  4. The call to click1() AFTER the Ajax call happens. I see an alert that the div is empty.

So it’s like the code is executing in the order it’s written but the DOM is not updating in the same order.


Edit 2 So the short answer is that putting the code in onSuccess is the correct place.

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:

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!     }   }); } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T14:38:57+00:00Added an answer on May 10, 2026 at 2:38 pm

    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 immediately returns. This means that the code immediately after it is normally actually executed before the onSuccess handler gets called (and before the AJAX request has even finished).

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 154k
  • Answers 154k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can't use partial classes. You could use inheritance, but… May 12, 2026 at 10:34 am
  • Editorial Team
    Editorial Team added an answer You can select count(*) from information_schema.tables if you have priviliges… May 12, 2026 at 10:34 am
  • Editorial Team
    Editorial Team added an answer the whole thing with setInterval is scope. the way your… May 12, 2026 at 10:34 am

Related Questions

Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.