1.不同的瀏覽器建立 XMLHttpRequest 對象的方法(通用函數)
<script type="text/javascript">
function ajaxFunction()
{
var 
xmlHttp;
try
    {
   // Firefox, Opera 8.0+, Safari
    
xmlHttp=new XMLHttpRequest();
    }
catch (e)
    {
  // Internet Explorer
   try
      {
    //IE 6.0+
      
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
   catch (e)
      {
      try
         {
        //IE 5.5+
         
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
         }
      catch (e)
         {
         alert("您的瀏覽器不支援AJAX!");
         return false;
         }
      }
    }
}
</script>
2.一個簡單的Ajax程式範例
<html>
<body>
<script type="text/javascript">
function ajaxFunction()
{
var 
xmlHttp;
try
    {
   // Firefox, Opera 8.0+, Safari
    
xmlHttp=new XMLHttpRequest();
    }
catch (e)
    {
  // Internet Explorer
   try
      {
      
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
   catch (e)
      {
      try
         {
         
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
         }
      catch (e)
         {
         alert("您的瀏覽器不支援AJAX!");
         return false;
         }
      }
    }
    xmlHttp.
onreadystatechange=function()
      {
    //4代表請求已完成
      if(xmlHttp.
readyState==4)
        {
         document.myForm.time.value=xmlHttp.
responseText;
        }
      }
    xmlHttp.
open("GET","time.asp",true);
    xmlHttp.
send(null);
}
</script>
<form name="myForm">
使用者: <input type="text" name="username" 
onkeyup="ajaxFunction();" />
時間: <input type="text" name="time" />
</form>
</body>
</html>
這是 "time.asp" 的代碼:
<%
response.expires=-1        //頁面不緩衝
response.write(time)
%>