PDA

View Full Version : Loading HTML and then adding to that new content



SirChick
10-15-2012, 06:03 AM
Hey

I have a script which is meant to add to the div, but i don't want to add the content via the "call_back" div otherwise im limited on the dynamic possibilities. So i'm wondering how i do a request then "continue" from that request on wards if that makes sense... (i added comment where i refer to):




function call_back(result,div_id){
document.getElementById(div_id).innerHTML = result;
}

function caller(url,cfunc)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=cfunc;
xmlhttp.open("GET",url,true);
xmlhttp.send();
}


function call_file(url,div_id){
caller(url,function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
call_back(xmlhttp.responseText,div_id);
}
});
}

window.onload = function() {

file = 'test.html';
div_id = 'test';
result = call_file(file,div_id);
call_file(file,div_id);
//something here to check "call_file" completed successfully then continue with code below
document.getElementById('test2').innerHTML = 'Hi'; //currently it says innerHTML is not found so i need to fix that

};


Any ideas on how to do this ?

jscheuer1
10-15-2012, 06:30 AM
The only way to know there has been success is here:


function call_file(url,div_id){
caller(url,function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
call_back(xmlhttp.responseText,div_id);
}
});
}

And of course anything in call_back() would only be done if there were success. You could pass in another argument telling it what to do. But in order to execute only upon success, it will have to be executed one of those two places, or somewhere one of them points to.

Also you have two id's, 'test' and 'test2'. Is there an actual test2 element? If not, that's why you're getting an error. If you do have a test2 element, it should work. But there could always be some other problem that's not readily apparent from the code you're showing. Do you actually want "Hi" or whatever to appear in a separate element?

If you want more help, please include a link to the page on your site that contains the problematic code so we can check it out.