By default, HTTP requests via XMLHttpRequest are asynchronous, which explains the need for a callback function. However, when you set the third parameter of the open() method to false, the request is a synchronous one, which means that the script execution is stopped until data comes back from the server. The following code makes use of that: Sending a Synchronous Request (xmlhttpsync.html)
Note that the whole code is executed only after the full page has been loaded; otherwise, the access to the output HTML element may fail. Warning Synchronous calls may be convenient, but from a usability and performance point of view, they should be avoided. The script execution stops completely while the call is made, which could turn into a real nightmare with slow connections or servers under a heavy load. |