AJAX Suggest: The JavaScript File
This page includes a link to a JavaScript page
<script src="ShowHint.js"></script>
which contains a function
showHint
as below.
If the input field is empty
(str.length==0
), the function clears the content of the
txtHint
placeholder and exits the function.
If the input field is not empty, the
showHint
function executes the following:
- Create an
XMLHttpRequest
object.
- Create the function to be executed when the server response is ready.
- Send the request off to a file on the server.
- A parameter (
q
) with the content of the input field is added to the URL.
01 | function showHint( str ) { |
02 | if ( str.length == 0 ) { |
03 | document.GetElementById( "txtHint" ).innerHTML = "" ; |
06 | if ( window.XMLHttpRequest ) { |
08 | xmlhttp = new XMLHttpRequest( ); |
12 | xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" ); |
14 | xmlhttp.onreadystatechange = function ( ) { |
15 | if ( ( xmlhttp.readyState == 4 ) && |
16 | ( xmlhttp.status == 200 ) ) { |
17 | document.GetElementById( "txtHint" ).innerHTML = |
21 | xmlhttp.open( "GET" , "GetHint.php?q=" +str, true ); |
|