0

I just found out that there is a variable called external exists in most browsers except IE. I have 2 question wrt to this

  1. what is external - http://jsfiddle.net/EVBjU/
  2. IE gives object doesn't support this property or method when i do console.log(external). how to fix this, considering it's just a variable

Thanks

aWebDeveloper
  • 36,687
  • 39
  • 170
  • 242

1 Answers1

2

"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.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241