0

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?

1
  • Just check if another function is defined? The task have exactly 0 relevance to jQuery. (Obligatory image: i.sstatic.net/ssRUr.gif) Commented Sep 18, 2013 at 11:06

1 Answer 1

3

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.
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your response let me ask you a quick thing how would you make one of t he script wait for the other if the function does not yet exists?
Aside from using AJAX, you could just wrap it inside setInterval(), and then use a similar check to the first example I posted. As soon as the function becomes defined, execute it and clear the interval.

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.