View Full Version : JavaScript Not Evaluating when called using XMLHTTPRequest

05-25-2005, 09:11 PM

I have a page with a div on it (titled 'mainDoc') and a serise of links,

When the user clicks on a link the following code returns the HTML source of another webpage to a string.

I then dump the string into the div using innerHTML

function getHeader(tab){
var xmlhttp=false;
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new
} catch (E) {
xmlhttp = false;
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();

var doc = "tab_content/"+tab+".php";
xmlhttp.open("GET", doc, true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
var main = document.getElementById('mainDoc');
var html = xmlhttp.responseText;
main.innerHTML = html;

The problem is this:

In the 'return string' (i.e var html ) there is some JS code.
This code does not get executed, ever...

so for example:

if var html contained


No alert would happen.

any ideas or help would be great!

05-25-2005, 11:04 PM
You'll probably have to extract it then eval() it.

Harry Armadillo
05-25-2005, 11:25 PM
Yeah, in IE you have to extract and eval(). In Gecko-browsers, you can create working script-elements using DOM-methods, but it probably isn't worth the bother.