I have plenty of JavaScript files, and take three as example: App.js, ChildApp.js(more than one),and AjaxManager.js, while AjaxManger.js is what I add.
ChildApp extends App, and App dependent on AjaxManager.
In App's constructors:
function App() {
this.ajaxManager=new AjaxManager();//Need AjaxManager.js to import, especially when new ChildApp is called.
....
}
ChildApp's constructor:
function ChildApp's() {
App.call(this); //
}
ChildApp.prototype = new App();
...
In the legacy code, there are tens of places where include App.js and I don't want to copy and paste the same code that includes AjaxManager.js before there.
But if I don't include AjaxManager well, I will have error when
var app=new ChildApp();//ReferenceError: AjaxManager is not defined
So I use below code IN THE BEGINNING of App.js:
if(typeof AjaxManager =='undefined'){
console.log('AjaxManager is not loaded , getting the script');
$.ajax({
url: .....//Url to load
dataType: "script",
async:false// How can I use async =true
});
}
But I have to use async:false for the request,which shows me a warning in the console that :Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience
Or I will have the error:
ReferenceError: AjaxManager is not defined
How can I use asynchronous request to load the script and sure things are working but will not break my existing functionality.
Update:
I tried
$.getScript(url)
in the beigining of App.js or in the begining of function App() before I ask this question but the same error, I guess something relative to the constructorApp
should be place in the callback but I don't know how.I hope that I can modify app.js or ajaxmanager.js only since there are too many places where call new App() or new ChildApp()