tags:

views:

45

answers:

2

Hi all,

Is there another code or a better one for this code:

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;
}

I tried a lot to use it for two divs in one page.. but it won't work for twice.. only once, so I want a better code for this thing and that works two times or more in one page, exactly I want it like a function with div id parameter and page too..

Thanks advance..

+1  A: 

Why not using some JS framework, like mootools or jQuery?

mbq
I used jQuery but it takes so much memory usage. And this this code is better without caching or memory usage
phplover
@phplover -- the core jQuery is only 24KB (smaller than most image you'll have on your site). Also it can be used from Google Code: http://code.google.com/apis/ajaxlibs/documentation/index.html#jquery, which means that it's likely that your users will already have it cached. Even if you're using SSL and your users need to download it from your site, that's really only necessary once if you allow it to be cached.
tvanfosson
@phplover You will end up using a JS framework in the end, if you want to be able to create your site cleanly / with minimum headaches
bobobobo
@tvanfosson -- this is not what I meant .. I meant that the jQuery reload think makes the browser lagging and slower, and this Ajax code works fine but doesn't work two times or more.. What I want is a function does the same thing.
phplover
@phplover -- that's not my experience with it. Perhaps, the problem lies with your code not jQuery?
tvanfosson
I'm using this code: var auto_refresh = setInterval(function (){$('#div1').load('page.php').fadeIn("slow");}, 3000); ... so what's the problem with it??
phplover
A: 

You should use jQuery for the job, it has a lot of nice AJAX functions.

What you might specifically need is: http://api.jquery.com/load/

DjDarkman
I know, but when i use setInterval() with it, it makes the browser too slow then goes to no responding.
phplover
Don't do that, instead use setTimeout like this:function loadContent() { $("#success").load("/not-here.php",function() { setTimeout(loadContent,50) });}This way you won't overload neither the browser nor the server.
DjDarkman
This works fine !! Thanks very much mate.
phplover