I”m stucked now
i”m building a web application that i want to implement ajax to improve the performence. Now problem is i actived the javascript to ask the server side script (in php) response me html code then i use .innerHTML to insert its inside a div tag which the id chosen … it worked well.
But now i want to active java scripts again with another function to connect to server side to get another html response code to insert in the previous html reponse code … and prob show up here is the document.getElementById(id) can”t find the assign div tag by id because some reason ( i guess because the previous return html code generate by server side and java script which should not appear in the html code so the document.getElementById can”t find out that div becuase that div tag actually not existed in the html code orginal (view by view source) ).
So in general my question is … is there a way to let us working with the responseText from server side and we can connect to server side to get new response and implement its on previous response base on ajax idea …
here is my buggy java scrips hope you can get more clear picture about what i”m asking right now
[CODE]
var xmlHttp;
var globalObj;
createXMLHttpRequest();
function createXMLHttpRequest()
{
if (window.ActiveXObject)
{
xmlHttp = new ActiveXObject(“Microsoft.XMLHTTP”);
}
else if (window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
}
/*
function createXML()
{
var options = document.getElementById(“catID”).childNodes;
var option = null;
var theValue;
for(var i=0; i<options.length; i++)
{
option = options[i];
if(option.selected)
{
theValue = option.value;
}
}
return theValue;
}
*/
function show_cat_list()
{
//createXMLHttpRequest();
//var catID = createXML();
xmlHttp.open(“GET”,’do.php?act=show’, true);
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.setRequestHeader(“Content-Type”, “application/x-www-form-urlencoded”);
xmlHttp.send(null);
}
function show_cat(catID)
{
//createXMLHttpRequest();
//var catID = createXML();
xmlHttp.open(“GET”,’do.php?act=show&catID=’+catID, true);
xmlHttp.onreadystatechange = handle;
xmlHttp.setRequestHeader(“Content-Type”, “application/x-www-form-urlencoded”);
xmlHttp.send(null);
}
function handleStateChange()
{
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
//parseResults();
//editResponse ();
globalObj = xmlHttp.responseText;
html_response();
}
}
}
function handle()
{
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
//parseResults();
//editResponse ();
html_response1();
}
}
}
function parseResults()
{
var responseDiv = document.getElementById(“returnBody”);
if(responseDiv.hasChildNodes())
{
responseDiv.removeChild(responseDiv.childNodes[0]);
}
var responseText = document.createTextNode(xmlHttp.responseText);
responseDiv.appendChild(responseText);
//alert(xmlHttp.responseXML);
//alert(responseText);
}
function editResponse ()
{
var response = xmlHttp.responseText;
if (response.indexOf(“ERRNO”) >= 0
|| response.indexOf(“error:”) >= 0
|| response.length == 0)
throw(response.length == 0 ? “Server error.” : response);
responseXml = xmlHttp.responseXML;
xmlDoc = responseXml.documentElement;
title = xmlDoc.getElementsByTagName(“title”)[0].firstChild.data;
//alert(title);
var responseDiv = document.getElementById(“serverResponse”);
if(responseDiv.hasChildNodes())
{
responseDiv.removeChild(responseDiv.childNodes[0]);
}
var responseText = document.createTextNode(title);
responseDiv.appendChild(responseText);
//responseDiv.appendChild(“<p>”+title+”</p>”);
}
function html_response ()
{
var obj = document.getElementById(“returnBody”);
obj.innerHTML = xmlHttp.responseText;
//obj.innerHTML = “hello world”;
}
function html_response1 (catID)
{
//globalObj = globalObj.documentElement;
alert(globalObj);
if (globalObj.getElementById(“catID_”+catID))
{
alert(“cool”);
}
else
{
alert(“****”);
}
var obj = document.getElementById(“catID_”+catID);
//obj.innerHTML = xmlHttp.responseText;
obj.innerHTML = “hello world”;
}