I want to load a Javascript file and execute it, using AJAX. I'm aware of solutions like jQuery's .getScript()
, but I do not want to use any library! I'm also aware of writing <script>
elements to the DOM, but as I said, I'm trying to achieve this with AJAX.
Here's a slimmed down version of my tries:
var http;
if(window.XMLHttpRequest){
http=new XMLHttpRequest();
}
http.open('get','libs/teh-lib.js',false);
http.onreadystatechange=function(){
if(http.readyState==4 && http.status==200){
alert(http.responseText);
}
}
Firebug shows the requests succeed, the right file is accessed, and a HTTP status 200 is shown. But the response seems to be empty. http.responseType
and http.response
seem to be empty, too. I also tried eval(http.responseText)
.