Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 这个Ajax代码需要更好的代码吗_Javascript_Ajax - Fatal编程技术网

Javascript 这个Ajax代码需要更好的代码吗

Javascript 这个Ajax代码需要更好的代码吗,javascript,ajax,Javascript,Ajax,此代码是否有其他代码或更好的代码: window.onload = refresh(); function refresh() { xmlHttp=GetXmlHttpObject() if (xmlHttp==null) { alert ("Browser does not support HTTP Request") return } var url="mypage.php" xmlHttp.onreadystatechange=stateChanged xmlHttp.op

此代码是否有其他代码或更好的代码:

window.onload = refresh();

function refresh()
{

xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 } 
var url="mypage.php"
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
} 
function stateChanged() 
{ 
 if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("div1").innerHTML=xmlHttp.responseText; 
a=setTimeout("refresh()",1000);

}
} 
function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}
我尝试了很多,用它在一页上写两个div。。但这两次都不管用。。只有一次,所以我想要一个更好的代码,在一个页面中工作两次或更多次,就像一个带有div id参数和page的函数一样


感谢advance.

为什么不使用一些JS框架,比如mootools或jQuery?

你应该使用jQuery来完成这项工作,它有很多很好的AJAX功能


您可能特别需要的是:

我使用了jQuery,但它占用了大量内存。这段代码在没有缓存或内存的情况下更好usage@phplover--核心jQuery只有24KB(比站点上的大多数图像都小)。它也可以从谷歌代码中使用,这意味着你的用户可能已经缓存了它。即使您使用的是SSL,并且用户需要从您的站点下载SSL,但如果您允许缓存SSL,这实际上只需要一次。@phplover如果您希望能够干净/最少地创建站点,您最终将使用JS框架headaches@tvanfosson我不是这个意思。。我的意思是jQuery重载思想会使浏览器变得滞后和缓慢,而这段Ajax代码可以很好地工作,但不能工作两次或更多次。。我想要的是一个函数做同样的事情。@phplover——这不是我的经验。也许,问题在于您的代码不是jQuery?我知道,但当我使用setInterval()时,它会使浏览器速度太慢,然后导致无响应。不要这样做,而是像这样使用setTimeout:function loadContent(){$(“#success”).load(/not here.php),function(){setTimeout(loadContent,50)}这样既不会使浏览器过载,也不会使服务器过载。