I'm trying to dynamically load the code of jquery.min.js and then the code of a custom JS file and use the functions from that custom file in my code, outside of the ajax call. Here is the code:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
eval(this.responseText);
$.post('/ajax.php?action=js', {
'action': 'custom'
}, function (response) {
eval(response);
// the response contains
// function custom1 () {...}
// ...
// function custom200 () {...}
});
}
xhttp.open("POST", "/ajax.php?action=js", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("action=jquery");
}
// some code here and then calling the function
function somefunc () {
// there is other code here, checking if the ajax response is present
// and then calling the function:
custom1();
}
// calling somefunc(), which checks and calls the "undefined" function
somefunc();
However I get Error: ReferenceError: custom1 is not defined
. The issue is most probably
not caused by the async, I check if a var created inside the response function is visible here and it is, if it's not setTimeout
until it is and then execute the function.
Does somebody know why is that and how can I make the function to be used outside the Ajax call (I checked it in it and there it is defined, but not outside)?
Note that there are some 200 odd functions there with different names I need to use in random parts of the code.