AJAX Example - AJAX Source
AJAX ʵÀý - AJAX Ô´Âë
The source code below belongs to the AJAX example on the previous pages.
ÏÂÃæµÄÔ´´úÂëÊÇǰһ¸öÒ³ÃæµÄ¡£
You can copy and paste it, and try it yourself.
Äã¿ÉÒÔ½«Ëü¸´ÖƲ¢Õ³Ìù£¬×Ô¼ºÀ´³¢ÊÔ¡£
The AJAX HTML Page
AJAX HTMLÒ³Ãæ
This is the HTML page. It contains a simple HTML form and a link to a JavaScript.
ÕâÊÇÒ»¸öHTMLÍøÒ³¡£Ëü°üÀ¨ÁËÒ»¸ö¼òµ¥µÄHTML±íµ¥ºÍ¹ØÁªJSµÄlink
<html> <head> <script src="clienthint.js"></script> </head>
<body>
<form> First Name:
<input type="text" id="txt1" onkeyup="showHint(this.value)"> </form>
<p>Suggestions: <span id="txtHint"></span></p>
</body> </html>
|
The JavaScript code is listed below.
JS´úÂëÔÚÏÂÃæ
The AJAX JavaScript
AJAX µÄ JS
This is the JavaScript code, stored in the file "clienthint.js":
ÕâÊÇJS´úÂ룬±»±£´æÔÚ"clienthint.js"ÎļþÖÐ
var xmlHttp
function showHint(str) { if (str.length==0) { document.getElementById("txtHint").innerHTML="" return } xmlHttp=GetXmlHttpObject() if (xmlHttp==null) { alert ("Browser does not support HTTP Request") return } var url="gethint.asp" url=url+"?q="+str url=url+"&sid="+Math.random() xmlHttp.onreadystatechange=stateChanged xmlHttp.open("GET",url,true) xmlHttp.send(null) }
function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { document.getElementById("txtHint").innerHTML=xmlHttp.responseText } }
function GetXmlHttpObject() { var objXMLHttp=null if (window.XMLHttpRequest) { objXMLHttp=new XMLHttpRequest() } else if (window.ActiveXObject) { objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP") } return objXMLHttp }
|
The AJAX server page is explained in the next chapter.
ÓйØAJAX·þÎñÆ÷¶ËÒ³ÃæµÄ½âÊÍ·ÅÔÚÏÂһƪÖС£