日期:2014-05-16  浏览次数:20796 次

ajax 获得页面的相应时间 支持ie 和ff
<html>

<title>无标题页</title>

<script type="text/javascript">
function $(name){
return document.getElementById(name);
}

function GetXmlHttpObject()
{

var xmlRequest;
    try{
     if(window.ActiveXObject)
     {
      var MSXML = new Array('MSXML2.XMLHTTP','Microsoft.XMLHTTP','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.5.0');
      for(var i=0;i<MSXML.length;i++)
      {
       try
       {  
        xmlRequest = new ActiveXObject(MSXML[i]);
        break;
       }
       catch(e)
       {
        xmlRequest = null;
       } 
      }
     }
     else if(window.XMLHttpRequest)
     {
      xmlRequest = new XMLHttpRequest();
      if(xmlRequest.overrideMimeType)
      {
       xmlRequest.overrideMimeType('text/xml');
      }
     } 
        if(xmlRequest == null)
        { // 异常,创建对象实例失败
        window.alert("不能创建XMLHttpRequest对象实例.");
        return false;
        }
    }
    catch(e){}
    return xmlRequest;
}

function ajaxTest(){
xmlHttp = GetXmlHttpObject();
if(xmlHttp==null)alert("error!!!!");
xmlHttp.open("GET","http://www.google.com.hk/",true);
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.send(null);
}
var time1;
var time2;

function stateChanged(){
if(xmlHttp==null)alert("error!!!!");
if (xmlHttp.readyState==1)
{
$("text1").innerHTML="Loading...";
time1=new Date();
}
if (xmlHttp.readyState==4)
{
time2=new Date();
var n=time2.getTime()-time1.getTime();
$("text1").innerHTML=("response time:"+n+"ms");


}
}

</script>

</head>
<body>

<input id="Button1" type="button" value="AJAX" onclick="ajaxTest()" /></div>
<div id="text1" style="width: 500px; height: 100px;">

</body>
</html>

?