Javascript ajax call on page onload

This is really easy using a JavaScript library, e.g. using jQuery you could write:

$(document).ready(function(){
    $.ajax({ url: "database/update.html",
        context: document.body,
        success: function(){
           alert("done");
        }
    });
});

Without jQuery, the simplest version might be as follows, but it does not account for browser differences or error handling:

<html>
    <body onload="updateDB();">
    </body>
    <script language="javascript">
        function updateDB() {
            var xhr = new XMLHttpRequest();
            xhr.open("POST", "database/update.html", true);
            xhr.send(null);
            /* ignore result */
        }
    </script>
</html>

See also:

  • Launching Code on Document Ready
  • jQuery ajax
  • http://msdn.microsoft.com/en-us/library/ms535874(VS.85).aspx

You can use jQuery to do that for you.

 $(document).ready(function() {
   // put Ajax here.
 });

Check it here:

http://docs.jquery.com/Tutorials:Introducing_%24%28document%29.ready%28%29