javaScript(ajax)(一)—— xmlhttp.onreadystatechange 执行顺序

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript" src="<%=request.getContextPath() %>/jQuery/jquery-1.8.3.js"></script>
<script language="javascript">
 function js_ajax(){
  var xmlhttp;
  if( window.XMLHttpRequest ){
   xmlhttp = new XMLHttpRequest();
   alert("IE6以上");
  }else{
   xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" );
   alert("IE5或IE6");
  }
  
  xmlhttp.onreadystatechange = function(){
   if (xmlhttp.readyState==4 && xmlhttp.status==200){
    document.getElementById("jQuery_div").innerText = xmlhttp.responseText;
    }
   }
  
  xmlhttp.open("GET","<%=request.getContextPath()%>/TestServlet?name=jQuery",true);
  xmlhttp.send();
  
  alert("xmlhttp.readyState:"+xmlhttp.readyState+"***xmlhttp.status:"+xmlhttp.status);
  
  /* xmlhttp.onreadystatechange = run();
  function run(){
   if (xmlhttp.readyState==4 && xmlhttp.status==200){
    document.getElementById("jQuery_div").innerText=xmlhttp.responseText;
      }
  } */
  
  /* xmlhttp.onreadystatechange = function(){                这段代码放在这不能执行?
   if (xmlhttp.readyState==4 && xmlhttp.status==200){
    document.getElementById("jQuery_div").innerText=xmlhttp.responseText;
    }
   }  */
  
 }
</script>
<body>
<form name="form" id="form" action="" method="post">
<input type="button" onClick="js_ajax()" value="javascript_ajax">
<div id="jQuery_div" ></div>
</form>
</body>
</html>

posted @ 2013-04-27 23:46  rocket_guo  阅读(378)  评论(0编辑  收藏  举报