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?
 
Ryan ChongCommented:
try do a try catch in your javascript to prevent error ? like:


function2() {

   try {

     function1();
 
   } catch (e) {}

}
0
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.