Call Javascript function only if it is defined

function1() is in "script1.js"
function2() is in "script2.js"
"Cumulative.jsp" includes "script1.js" & "script2.js"
I'm calling function1() from function2(). I want to check the availability of function1() before calling it from function2().

i.e even if I don't include "script1.js" in "Cumulative.jsp", function2() should not give error while calling function1()
LVL 6
Ravi KallaTechnology LeadAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
try do a try catch in your javascript to prevent error ? like:


function2() {

   try {

     function1();
 
   } catch (e) {}

}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Om PrakashCommented:
You can check if a function exists of not
<script language="javascript" type="text/javascript">
function test() {
}

if(window.test) {
  alert('test exists');
}
else {
  alert('test does not exists');
}
</script>
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.