This ajax call works, it will update the permission and the caption of the image correctly. The issue I’m having is that Alert() won’t run after the script updates. Why is that? Is it not possible to have new javascript come in with an ajax load?
In any case, I think what you should do is not to try and send script along with the JSON, but instead have the alert already in place and only fire it if the JSON returns a specified value.
So how would I have an Ajax loaded script trigger a function already loaded inside of the DOM? For example, lets say I wanted it to alert(‘Image has been updated’), if the loaded script can’t trigger an alert, how can it trigger any other function in the DOM?
Here’s a simple function for adding scripts to the page so that you can add as many scripts as you like by calling that function as shown in the last line simply by supplying the filename (and path if there is one).
var addJS = function(nm) {
var s = document.createElement('script');
s.type='text/javascript';
s.src=nm;
document.getElementsByTagName('body')[0].appendChild(s);
}
addJS('newScript.js');
See also http://www.felgall.com/jstip128.htm which at the bottom gives an example where scripts dynamically replace one another so that which one runs depends on when you call it.
A significant problem is that you have both a function and a variable with the same name.
When the onreadystatechage function is triggered, which of the ajax function or variable will it call? Give them different names and you will not have any confusion there.
You are also loading an entire web page inside of an innerHTML. so the resultant page will be completely broken.
Your test2.php should only contain the HTML that you want loaded into the innerHTML.
JavaScript loaded into innerHTML doesn’t necessarily run either. If you want to make sure JavaScript added to the page and runs then simply add the script tag to the page directly from the JavaScript (no ajax call required).