HTML and CSS Reference
EXAMPLE 18.6 ( CONTINUED )
} //End getText() function
<span style="cursor: pointer; text-decoration: underline"
Fetch text from a file
<div id=data class="divStyle">
A new XMLHttpRequest object, called ajaxRequest , is created (code is in “ajaxRe-
quest.js”, Example 18.8).
The callback function is defined and assigned to the onreadystatechange event
handler used to monitor the state of the server.
When the readyState of the server is 4, the request is complete. If the HTTP status
is OK (200), the program proceeds.
The getElementById() method will return a reference to the div container named
“data” on line 8. The ajaxRequest.responseText property contains the ASCII text
that was returned. The innerHTML property will put that text in the < div > con-
The XMLHttpRequest object's open() method initializes the object. The request will
be sent as an HTTP GET request to get data from a file called “ajaxtext.txt”.
(When using Internet Explorer the URL was http://localhost/ajaxtext.txt with
Firefox just the name of the file was given.)
The request is sent to the server.
When the user clicks the underlined text defined by the span tag, the getText()
function will be launched. Its argument is the name of the text file that will be pro-
cessed, shown in Example 18.7.
This is the <div> container where the results of the HTTP request will be dis-
played. See Figure 18.14.
The Text File—“ajaxText.txt”
Once upon a time there were
three little bears.