I have an index.php file that loads other php files via this javascript/ajax code:
function AJAX(elementID,url,showStatus){
var httpObject;
if (window.ActiveXObject) {
httpObject = new ActiveXObject("Microsoft.XMLHTTP");
}
if (window.XMLHttpRequest){
httpObject = new XMLHttpRequest();
}
else {
alert("Your browser does not support AJAX.");
}
if (httpObject != null) {
httpObject.onreadystatechange = function() {
if (elementID != false){
if (httpObject.readyState == 4 && httpObject.status == 200) {
document.getElementById(elementID).innerHTML= httpObject.responseText;
}
}
}
httpObject.open("POST",url,true);
httpObject.send(null);
}
}
so for example I would load a file in inxex.php by:
<script>
AJAX("updateThisDiv", "/includes/contentpage.php", false)
</script>
which would paste the contents of “contentpage.php” into the div “updateThisDiv” but now if I have any javascript on “contentpage.php”, it will not run, is there any way to do this?
I have looked at this: http://www.javascriptkit.com/script/script2/ajaxpagefetcher.shtml
but its not exatcly what I was looking for.
I want to be able to update a section of my page without reloading the entire page and javascript must run
If you want to load Javascript on demand. This can be done by dynamically creating script tag. This pattern illustrated in Stoyan Stefanov book – Javascript Patterns
This snipped from the book:
Write a require function. Then call it like this:
Sample require function:
Live example found in http://www.jspatterns.com/book/8/ondemand.html
@Edit: more details specific to your case. I will try to make things simple:
Create four files:
index.php
content.php
content.js
code.js