ajax的一个运用 XMLHttpRequest
script2.html文件代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Ajax Demonstration</title><style>.displaybox { width:150px; background-color:#FFFFFF; border:2px solid #000000; padding:10px; font:24px "微软雅黑"; margin:auto;}</style><script type="text/javascript">function getXMLHTTPRequest() { try { req = new XMLHttpRequest(); } catch(err1) { try { req = new ActiveXObject("Msxml2.XMLHTTP"); } catch(err2) { try { req = new ActiveXObject("Microsoft.XMLHTTP"); } catch(err3) { req = false; } } } return req;}var http = getXMLHTTPRequest();function getServerTime() { var myurl = "telltimeXML.php"; myRand = parseInt(Math.random() * 999999999999999); var modurl = myurl + "?rand=" + myRand; http.open("GET", modurl, true); http.onreadystatechange = useHttpResponse; http.send(null);}function useHttpResponse() { if (http.readyState == 4) { if (http.status == 200) { var timeValue = http.responseXML.getElementsByTagName('timenow')[0]; document.getElementById('showtime').innerHTML = timeValue.childNodes[0].nodeValue; } else { document.getElementById('showtime').innerHTML = '<img src="anim.gif">'; } }}</script></head><body style="background-color:#CCCCCC; text-align:center"><h1>Ajax Demostration</h1><h2>Getting the server time without page refresh</h2><form><input type="button" value="Get Server Time" /></form><div id="showtime" class="displaybox"></div></body></html><?phpdate_default_timezone_set('PRC');echo '<?xml version="1.0" encoding="utf-8" ?><clock1><timenow>'.date("H:i:s").'</timenow></clock1>';?>