Partially change a page via Javascript

Asked

Viewed 64 times

0

I wanted this code to just update a part of the site and not the whole site.

Because when I run the function on a div, it updates the whole site.

<script type="text/javascript">
function Ajax(){
  var xmlHttp;
  try { xmlHttp=new XMLHttpRequest();// Firefox, Opera 8.0+, Safari }
  catch (e){
    try{ xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer }
    catch (e){
      try{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
      catch (e){
        alert("No AJAX!?");
        return false;
      }
    }
  }

  xmlHttp.onreadystatechange=function(){
    if(xmlHttp.readyState==4){
      document.getElementById('ReloadThis').innerHTML=xmlHttp.responseText;
      setTimeout('Ajax()',10);
    }
  }
  xmlHttp.open("GET","index.php",true); // aqui configuramos o arquivo
  xmlHttp.send(null);
}

window.onload=function(){
  setTimeout('Ajax()',10); // aqui o tempo entre uma atualização e outra
}
</script>
  • 1

    This code should update only the element with id="ReloadThis", what’s going on?

  • To begin I apologize Guilherme, then as I do only to update the div and not the whole page?

1 answer

-2

I believe it is because you put the function inside the onload of the page. Try to put inside a setInterval without onload, like this:

var interval = setInterval(Ajax(), 10);

  • Good afternoon I did it and the mistake continues.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.