On page load I am calling the function LinkPages() and am going to be adding its HTML to a a DIV.
MasterPage.aspx
function LinkPages()
var xmlhttp;
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) {
alert(xmlhttp.responseText);
}
}
xmlhttp.open("GET", "Page1.aspx", true);
xmlhttp.send();
}
The problem that I ran into is Page1.aspx also has a XMLHttpRequest in function GetMessage that is never being called. GetMessage() is
suppose to set the innerHTML of a span on Page1.aspx and it is not being called.
Page1.aspx
function GetMessage()
var xmlhttp;
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("lblMessage").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "wfrmHelper.aspx?action=message", true); --returns string
xmlhttp.send();
}
Is it possible to call javascript on the page in the XMLHttpRequest?
In your first XHR on MasterPage.apsx you are calling the server and getting some HTML back (and I am guessing that this Javascript GetMessage is mixed in on that page somewhere). Unless you append your XHR’s responseText to the document, the JavaScript will never get evaluated. So… in LinkPages() you need to add the response from your XKR call to the document, and then the GetMessage function will be evaluated by the browser. Until it gets added, it is only a String in memory. It doesn’t actually turn into script until you place it on the DOM. I hope this makes sense.