I have some code that uses AJAX to dynamically load some content into a div tag while the page remains there still, no reload flickering. Let's say that content is some JavaScript code. How can I run the code itself from the loader page?
Just to demonstrate it via a simple example what do I need to do to display the 'vazze' variable's value?
Sample code for loading page (loader.html) -----------------
<script src="/pulltree.js"></script>
...
<table cellpadding=0 cellspacing=0 border="0">
<tr><td><input type="radio" name="CP" value="0" onfocus="showTree('')" checked><%=getMessage("AccessAll","CompanyName")%></td></tr>
<tr><td><input type="radio" name="CP" value="1" onfocus="showTree(1)"><%=getMessage("SelectGroupAcc")%><div id="txtTreeLocation"></div>
<script language=javascript>
<!--
document.write(vazze);
//-->
</script>
...
Code for pulltree.js -----------------------------
var xmlHttp
function showTree(str)
{
if (str.length==0)
{
document.getElementById("txtTreeLocation").innerHTML="";
return;
}
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url="gettree.asp";
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
function stateChanged()
{
if (xmlHttp.readyState==4)
{
document.getElementById("txtTreeLocation").innerHTML=xmlHttp.responseText;
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
Code for gettree.asp -----------------------------
...
<p>This is the text I can see</p>
<script language=javascript>
var vazze = 1111;
</script>
...
There is a simple document.write there that breaks the code, because vazze is not there yet, otherwise I can see the text I pull in. Maybe I need to find a way to check if the innerHTML stuff is already loaded and then I can run the document.write or? What do you guys think?
Just to demonstrate it via a simple example what do I need to do to display the 'vazze' variable's value?
Sample code for loading page (loader.html) -----------------
<script src="/pulltree.js"></script>
...
<table cellpadding=0 cellspacing=0 border="0">
<tr><td><input type="radio" name="CP" value="0" onfocus="showTree('')" checked><%=getMessage("AccessAll","CompanyName")%></td></tr>
<tr><td><input type="radio" name="CP" value="1" onfocus="showTree(1)"><%=getMessage("SelectGroupAcc")%><div id="txtTreeLocation"></div>
<script language=javascript>
<!--
document.write(vazze);
//-->
</script>
...
Code for pulltree.js -----------------------------
var xmlHttp
function showTree(str)
{
if (str.length==0)
{
document.getElementById("txtTreeLocation").innerHTML="";
return;
}
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url="gettree.asp";
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
function stateChanged()
{
if (xmlHttp.readyState==4)
{
document.getElementById("txtTreeLocation").innerHTML=xmlHttp.responseText;
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
Code for gettree.asp -----------------------------
...
<p>This is the text I can see</p>
<script language=javascript>
var vazze = 1111;
</script>
...
There is a simple document.write there that breaks the code, because vazze is not there yet, otherwise I can see the text I pull in. Maybe I need to find a way to check if the innerHTML stuff is already loaded and then I can run the document.write or? What do you guys think?