How can I verify that a JavaScript function is loaded or exists on a page before calling it?

Can someone give me a piece of JavaScript code with which I can determine if the JavaScript function is loaded in my aspx web page or exists before it is called?

thank

+5
source share
3 answers

This will check if your function is defined.

if (typeof functionName === 'function') {
  alert('loaded');
}

Take a look.

+11
source

You can explicitly verify that this is a function before calling.

if (typeof(functionName) == "function")
    functionName();
+1
source

loaded?

, - onload, , . , ​​ , typeof:

// Check the type of "myRandomFunction"
// Note: typeof is the only way you can use undeclared variables without raising an exception
if (typeof myRandomFunction === 'function') {
    myRandomFunction()
}
+1

All Articles