<script type="text/javascript"> window.onload = initPage; function initPage() { var btn = document.getElementById("username").onblur = checkUsername; } function checkUsername() { request = createRequest(); if (request == null) { alert("没有请求对象!"); } var url = "DetailForm.aspx?username=" + escape(document.getElementById("username").value);//向何处发请求 request.onreadystatechange = showUsernameStatus;//函数引用,设置回调函数 request.open("GET", url, true); //请求方式,true表示将异步的发送请求 request.send(null);//没有随请求发送额外东西 } function showUsernameStatus() { if (request.readyState == 4) { if (request.status == 200) { document.getElementById("username").value = request.responseText; } } } function createRequest() { try { request = new XMLHttpRequest(); } catch (tryMS) { try { request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (otherMS) { try { request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (failed) { request = null; } } } return request; } </script>
第二章和第一章内容几乎一样,可以把有共同点的代码提取出来,以后用。