I have a page being ajaxed in, but the document ready code will not run. I'm running in IE7. I get no alert, but "Page is here" shows fine. The method "ajax" is a utility method I wrote. It works fine for all my ajax calls, so I know that's not the issue. I just can't get my javascript to run. Why would my javascript not be running?
AJAX Page
<script src="Utilities/Javascript/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
alert("RUN PLEASE");
</script>
Page is here
Main Page
<html>
<head>
<script src="Utilities/Javascript/Utilities.js"></script>
<script src="Utilities/Javascript/jquery-1.7.1.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function () {
ajax("scrolltest.aspx", "divy2");
});
</script>
<div id="divy2">
</div>
</body>
</html>
ajax method from Utilities.js
/*
* Ajax page loads with url and updates element's innerHtml
* @param url : URL to call for ajax page load
* @param element : Element ID to be updated
*/
function ajax(url, element) {
var ajx;
if (window.HXMLHttpRequest) {
ajx = new XMLHttpRequest();
}
else {
ajx = new ActiveXObject("Microsoft.XMLHTTP");
}
ajx.open("GET", url, true);
ajx.setRequestHeader("X-Requested-With", "XMLHttpRequest");
ajx.onreadystatechange = function () {
if (ajx.readyState == 4 && ajx.status == 200) {
document.getElementById(element).innerHTML = ajx.responseText;
}
else if (ajx.readyState == 4 && ajx.status == 400) {
alert("Page Error. Please refresh and try again.");
}
else if (ajx.readyState == 4 && ajx.status == 500) {
alert("Server Error. Please refresh and try again.");
}
else if (ajx.readyState == 4) {
alert(ajx.status + ":" + ajx.statusText);
}
}
ajx.send();
}
Javascript from an "AJAX" request will not be processed unless you call eval on it - which is not really the best idea
edit this answer is a little too brief - there are ways to native force execution - refer to this more detailed and similar question / answer
How to Force Javascript to Execute within HTML Response to Ajax Request
Scripts which have no defer
attribute won't be executed when inserted via innerHTML (MSDN).