PDA

View Full Version : AJAX refreshing div problem


2dan
03-27-2010, 09:57 PM
I have some ajax code which is refreshing a div of mine, however when it refreshes the div it places my index page inside that div, I know why but I don't know how to simply make it refresh that div without placing the index page inside it?


function Ajax(){
var xmlHttp;
try{
xmlHttp=new XMLHttpRequest();
}catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
alert("error");
return false;
}
}
}
xmlHttp.onreadystatechange=function(){
document.getElementById('refreshed_div').innerHTML=xmlHttp.responseText;
setTimeout('Ajax()',1000);
}
xmlHttp.open("GET","index.html",true);
xmlHttp.send(null);
}
window.onload=function(){
setTimeout('Ajax()',1000);
}


I don't know what I need to place where is says


xmlHttp.open("GET","index.html",true);


so that it displays the information within the div when its refreshed rather then the index page

any help would be greatly appreciated!

A1ien51
03-27-2010, 10:08 PM
Because you are fetching the entire document. If you only want a part of it you only return that partial piece.

Eric

2dan
03-27-2010, 10:11 PM
Thankyou Eric, I understand the problem and that I am returning the index page within that div when it is refreshed. The issue is I have no idea what I put there in place of index.php so that the div is refreshed with the content of the div rather then the index, if that makes sense?

A1ien51
03-27-2010, 10:28 PM
You link to a php page that only has the content that you want.

Eric

hgs
03-28-2010, 11:17 AM
See also here

http://www.codingforums.com/showthread.php?t=187047

You will find a link to an exampel there.

Regards

dspurg7310
04-29-2010, 09:22 AM
I am currently using this Ajax script and it works great. I have only one issue. I would like to run this script for two different div tags on the same page. So in this instance, the Ajax script is placed in a sepperate js file with two different txt files to access for two different div tags. One for something at the top of the page, and another for completely different content elsewhere on the page. The js files are too much alike and I can only get one of the div tags to load and the other does not. I have changed all of the Ajax() functions for the second script to Ajax2() in an effort to make the scripts different and not counter eachother but they still do. What other changes can I make to the script so they do not affect eachother?