2

I am calling a script using ajax. I want to show the progress of script execution on the page.

I mean I want to display the following messages in a div progressively:

//after execution of section 1
Section 1 of script executed successfully.
//after execution of section 2
Section 2 of script executed successfully. 
//after execution of last section
Script execution complete.

The method I am using is displaying all the 3 messages only after the complete execution of the script.

var script1_ajax = new XMLHttpRequest();

    script1_ajax.onreadystatechange=function() {            
        if (script1_ajax.readyState==4 && script1_ajax.status==200) {

            document.getElementById('result').innerHTML = script1_ajax.responseText;

        }
    }

I don't want to break my script into multiple scripts.

Please suggest how to do this.

3
  • What are you using on the server? AJAX typically will only return responesText when the page/script has finished loading (you cannot usually do what you are proposing). Commented May 23, 2012 at 7:55
  • 1
    @jezternz: Actually, you can usually do this. Commented May 23, 2012 at 8:01
  • 1
    Really! I apologize, I was under the impression that long polling just mean't the server held an ajax connection open until it had completed its request, but apon further reading I better understand it, cheers! Commented May 23, 2012 at 9:38

1 Answer 1

3

There are various techniques for keeping a connection to the server open and getting interim updates from that connection, which take the collective term "comet," which is described here. Long-polling, hidden iframes with chunked blocks, XHR on some browsers, etc. One or more of those may be useful to you. Note that different techniques work with different browsers, and each has upsides and downsides.

The situation will be rather better in a few years, when we can rely on web sockets, but for now that's not an option unless you can restrict use of your site/application to the few browsers that currently support them.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.