Check if function a from one.js from two.js exists

There is external js that I call to the main page. This external file has a function

function fruitLib(){
   //do stuff
}

I have another external js, and there is another function.

function price(){
   //do stuff
}

Now, how can I check if there is fruitLib()before the call price()? Below I try, but it does not work (possibly because both files are external files).

if (typeof fruitLib== 'function') { 
  price(); 
}
+4
source share
1 answer

Assuming it fruitLib()is in the foo.js file and price()in the bar.js, do this in the area in which you include the JavaScript files:

<script src="foo.js">
<script src="bar.js">

So you know what is fruitLib()loading up to price().


: Javascript, .

+2

Source: https://habr.com/ru/post/1617963/


All Articles