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

14
  • see stackoverflow.com/questions/10694341/… Commented Jul 22, 2013 at 10:30
  • What do you mean by "fix this"? Simply removing the logging statement will remove the error. Commented Jul 22, 2013 at 10:31
  • @FabrizioCalderan but IE10 is giving giving error for it Commented Jul 22, 2013 at 10:31
  • @DavidPärsson i know that but how do i fix a "object doesn't support this property or method" in general Commented Jul 22, 2013 at 10:32
  • There is no fix, since what you're doing is not supported. The only fix is to not do it. Commented Jul 22, 2013 at 10:35

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

can hasOwnProperty be used for functions too
Yes. In general terms, a JS object's "methods" are really just properties that happen to refer to functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.