"but how do i fix a "object doesn't support this property or method" in general"
Given an object obj
, you can test whether property/method prop
exists with:
if ("prop" in obj) {
// do something with obj.prop
}
...noting that the in
operator will check inherited properties too. To check only for direct properties use:
if (obj.hasOwnProperty("prop")) {
// do something with obj.prop
}
"is there a way to check if the variable external
exists"
In the case of the external
property you mentioned, it will be a property of window
if it exists, so:
if ("external" in window) {
// do something
}
This x in window
technique works for global variables including ones provided by the browser and user-defined ones. It doesn't work on local variables.