4

I am using Cordova GA plugin to track events, but when testing in my browser I keep getting:

ReferenceError: plugin is not defined

I tried many ways to check if the plugin has been defined, but it still throws this error. How can I properly check if the plugin has been defined before I invoke the function?

1

2 Answers 2

5

To check if a JavaScript variable is undefined you can use

if (typeof myVariable === 'undefined') {
  console.log('The variable is undefined.');
}
Sign up to request clarification or add additional context in comments.

Comments

1

You may simply put a check before running your code:

if (window.plugin) {//I had assume that `plugin` is in global scope
  alert('plugin');
} else {
  alert('no plugin');
}

Using typeof

if('undefined' === typeof plugin) {
  alert('no plugin');
} else {
  alert('plugin ...');
}

Comments

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.