Marc
Marc

Reputation: 493

Check if another script is loaded without using JQUERY

I have two different that are somewhat depending on eachother (i.e one of the script has a function that calls the other script)

Now i wont go into any details other than saying i am not able to load one before the other.

Because of this i need to make a check in the scripts to check if the other script is loaded (So that the scripts "waits" for eachother).

My Boss does NOT want me to use Jquery so i was wondering is there a way in Plain javascript to check if another script is loaded? And if so how?

Upvotes: 0

Views: 57

Answers (1)

BenM
BenM

Reputation: 53246

You could check if the function is already defined in the script which calls it, by doing something like:

if(typeof functionName === 'function') 
{
    // function exists so call it
}

That having been said, you'd be much better off to use proper error handling, by wrapping your code inside a try-catch block. So for example within the script that calls a function defined in your external file, you could try:

try 
{
    functionName();
}
catch(e) 
{
    // the call returned an error (likely because functionName() isn't defined).
    // Handle the error nicely.
}

Upvotes: 3

Related Questions