Ajax调用的另一种样式

I am doing some experiments with AJAX calls in pure Javascript, no JQuery. I wonder if I can populate a DIV tag like this:

<script type="text/javascript">
 function call_test() {
   document.getElementById("myId").innerHTML = ajax_call("example.php?id=1") ;
 }
</script>
<body>

<input type="button" onClick="call_test()" value"Test">

<div id="myId">Result should be here</div>

The problem is how to return the result from ajax_call? My code is as follows, but not working:

function ajax_call(remote_file)
{
    var  $http, 
    $self = arguments.callee ;
    if (window.XMLHttpRequest) {
        $http = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
            $http = new ActiveXObject('Msxml2.XMLHTTP');
        } catch(e) {
            $http = new ActiveXObject('Microsoft.XMLHTTP');
        }
    }
    if ($http) {
        $http.onreadystatechange = function()  {
            if (/4|^complete$/.test($http.readyState)) {
                return http.responseText ; // This only return undefined
            }
         };
         $http.open('GET', remote_file , true);
         $http.send(null);
    }
}

Remote file:

<?php
  echo "<h1>Jus an experiment</h1>";
?>

It will not work because of the asynchronous nature of AJAX requests. The ajax_call method will return before the server responds with the html that is why you are getting undefied.

The solution here is to use a callback to do the post processing of ajax response as shown below.

function ajax_call(remote_file, callback) {
    var $http, $self = arguments.callee;
    if (window.XMLHttpRequest) {
        $http = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
            $http = new ActiveXObject('Msxml2.XMLHTTP');
        } catch (e) {
            $http = new ActiveXObject('Microsoft.XMLHTTP');
        }
    }
    if ($http) {
        $http.onreadystatechange = function() {
            if (/4|^complete$/.test($http.readyState)) {
                if (callback)
                    callback(http.responseText);
            }
        };
        $http.open('GET', remote_file, true);
        $http.send(null);
    }
}

And

function call_test() {
    ajax_call("example.php?id=1", function(html) {
        document.getElementById("myId").innerHTML = html
    });
}