It's not something that AJAX can do out of the box, and it's a little dangerous to enable such behaviour. The reason for this (as I understand) is the same reason you can't do cross domain requests, in defense against cross-site scripting attacks.
This is how I know how to do it, there is probably a better way as doing this is very ill advised.
Basically you have to extract the JS from the response string of the XMLHTTP object, then eval the string. This is a bad idea because of the eval function, it's just one letter way from being evil.
This function finds the <script> open and closing tags, then parses everything inbetween.
function parseStringEvalJS(input){
var str = input;
var startTag = str.indexOf("<script");
var startLen = 8;
var endTag = str.indexOf("</script");
var endLen = 9;
var startChar = startTag + startLen;
var endChar = endTag - startChar;
//check if we have code to parse.
if(startTag == -1 || endTag == -1){
return true;
}else{
//parse our whats in <script> tags.
var code = str.substr(startChar, endChar);
try{eval(code);}
catch(err){}
//get our new str minus the code.
var newStr = str.substr(endTag+endLen);
parseStringEvalJS(newStr);
}
return true;
}
Where I handle the request object, I add this little bit.
if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
obj.innerHTML = XMLHttpRequestObject.responseText;
parseStringEvalJS(XMLHttpRequestObject.responseText);
}
So that part is just like normal, but after I load the content into my html object, I then run my JS Parser against it.
I'm extremely interested in other replies to this thread, as I know the method I just posted is less than ideal.