I am trying to load a class dynamicly and check if it was loaded before.
the code below is not working, any idea why?
I am unable to understand why is this part not working:
if (typeof window['Car'] == 'undefined')
full source:
if (typeof window['Car'] == 'undefined') {
alert("Car class not loaded!");
}
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.src = 'http://m.uploadedit.com/bbtc/1572125047736.txt';
script.type = 'text/javascript';
head.append(script);
setTimeout(function(){
if (typeof window['Car'] == 'undefined') {
alert("Even After X seconds, Car class not loaded!");
}else{
alert("After X seconds, Car class loaded!");
}
}, 3000);
Update I am adding the solution source here, just in case the fiddle is lost:
function testClass(cls) {
return eval("typeof " + cls + " === 'function'");
}
if (!testClass('Car')) {
console.log("Car class not loaded when starting as expected!");
}
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.src = 'http://m.uploadedit.com/bbtc/1572125047736.txt';
script.type = 'text/javascript';
head.append(script);
setTimeout(function(){
if (!testClass('Car')) {
alert("Even After X seconds, Car class not loaded!");
}else{
alert("After X seconds, Car class loaded!");
}
}, 3000);
And this url (http://m.uploadedit.com/bbtc/1572125047736.txt) contains a simple class:
class Car {
constructor(brand) {
this.carname = brand;
}
get cnam() {
return this.carname;
}
set cnam(x) {
this.carname = x;
}
}