ing0 ing0 - 5 months ago 14
PHP Question

Running javascript in a page loaded by xmlhttprequest

In my javascript method I'm calling a xmlhttprequest to grab some data and display it in a div called 'myDiv' (index.php)

function combo_change(theid)
{
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=function()
{
if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","anotherpage.php?id=" + theid,true);
xmlhttp.send();
}


Now, I'm trying to call some javascript within the page "anotherpage.php", but it does not want to work. As a test I'm just doing a
document.write('hello');
. If I load the page directly it will show, but when using the code via xmlhttp open it will not.

I can assume that when loading like this, the javascript is not being run. So, is there anyway I can make it run?

Answer

Write your JavaScript code inside

if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
  document.getElementById(\"myDiv\").innerHTML=xmlhttp.responseText;
  //Your js code 
}