I have a HTML that uses this function for loading another PHP with Ajax:
function LoadContent(n,func)
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState== 4 && xmlhttp.status == 200)
{
document.getElementById("div-content").innerHTML=xmlhttp.responseText;
if(typeof(func)==='undefined')
{
// nothing
}
else
{
eval(func);
}
}
}
xmlhttp.open("GET",n,true);
xmlhttp.send();
}
So far, all OK. The PHP to be loaded generates a HTML which contains a
<script>
function foo() {
alert('hello');
}
</script>
And the original HTML calls the loader with javascript:LoadContent('file.php','foo()')
, but foo() is not called. The eval() line says "no such function" in Chrome.
How could I manage it so, after Ajax loading, the JavaScript function which would reside in the loaded html would be executed?