SteveV
SteveV

Reputation: 441

Verify JS Method Exists Before Calling

I am referencing a third party library in a SCRIPT tag and it's getting blocked by Ad Blockers. This results in errors because the library isn't loaded and JS is trying to use methods that now do not exist. What's the best way test if a method exists before calling it?

EXAMPLE

<script type="text/javascript" src="https://path-to-api"></script>

In the console, the JS API is blocked as such: gpt.js:1 GET https://path-to-api net::ERR_BLOCKED_BY_CLIENT

Upvotes: 0

Views: 79

Answers (1)

anacampesan
anacampesan

Reputation: 105

you can check if a function exists by doing the following:

if (yourLib && typeof yourLib.method === "function") { 
 // it exists / is not undefined
}

Upvotes: 1

Related Questions