2
I tried to make a code so that when I changed something in a div, I would automatically change the page in real time without having to refresh or anything like that, however when I change the content of the div if it happens to be F5, the information appears repeated.
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
}
<div style="background-color:#00CED1;" id="ReloadThis"></div>
teste
When executing the code if you give F5 on the page appears 2x the message "test".
I was wondering if there was any way to fix this.
Thank you
Tell me something, because even though I put things outside the div, the site continues to update things that are not inside the div in real time?
– Gonçalo
your AJAX basically overrides the content of
div#ReloadThis
, I believe that yourindex.php
have only the textteste
, then at the end of the AJAX request, it will update the contentdiv#ReloadThis
with the textteste
and keep the textteste
out of the intact div... if you write in thearquivo.php
hello world
, then on your page will appearhello world teste
.– Tobias Mesquita
How do I only update the div in real time without adding anything else?
– Gonçalo
Gonçalo, your div is already being updated in real time, the only reason to appear the text
teste
2x, is pq the same is initially outside the div to be updated.– Tobias Mesquita
How do I correct that?
– Gonçalo
When I put something outside the div, this thing continues to be updated in real time, even if not in the div and when I give F5 appears 2x.
– Gonçalo
Gonçalo, I believe you have two html/php files, the first one where this script is together with the div and the second with the page fragment with the content to be updated in real time... your script updates only the content of
div#ReloadThis
on its first page with the contents of Fragment (second page), if it is not behaving this way, check that all its html tags are closing in the correct way.– Tobias Mesquita
You can simplify my text, I’m a little new in the field and I don’t understand some terms.
– Gonçalo