Ajax 日记(一)

来源:互联网 发布:如何利用网络赚钱 编辑:程序博客网 时间:2024/05/22 13:12
 /*************
 * AjaxLib.js
 *************/
function XmlHttpRequestObject()
{
 var obj = null; 
 if(window.ActiveXObject)
 {
  try
  {
   obj = new ActiveXObject("Msxml2.XMLHTTP");
  }
  catch(e1)
  {
   obj = new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
 else if(window.XMLHttpRequest)
 {
  try
  {
   obj = new XMLHttpRequest();        
  }
  catch (e2)
  {
   obj = null;
  }  
 } 
 if(!obj)
 {
  window.alert("The XmlHttpRequest is not supported!");
 } 
 return obj;  
}
 
/*************
 * index.htm
 *************/
<!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>
  <title> new document </title>
  <meta name="generator" content="editplus" />
  <meta name="author" content="" />
  <meta name="keywords" content="" />
  <meta name="description" content="" />
  <script type="text/javascript" src="AjaxLib.js"></script>
  <script type="text/javascript" language="javascript">
  var ajaxObj = XmlHttpRequestObject();
  function getRemFile(objId)
  {
   var obj = document.getElementById(objId);
   if(obj && obj.value != "")
   {
    try
    {
     ajaxObj.open("GET", obj.value, false);
     ajaxObj.send();
     window.alert(ajaxObj.responseText);
    }
    catch(e)
    {
     window.alert(e.message);
    }
   }
   else
   {
    window.alert("Empty Input value,pls check!");
   }
  }
  </script>
</head>
<body>
 <div style="text-align:center;">
  Pls enter the remote file URL: <input id="fileUrl" type="text"/><br />
  <input id="fileBut" type="button" value="Get File!" onclick="javascript:getRemFile('fileUrl');"/>
 </div>
</body>
</html>