What is the function that always exists and is not first calling function?
Answers
Answered by
2
When using scripts that are shared between different areas of a site, there may be cases where a function is called that doesn't exist. It makes sense on one page (the dependency is there) but not another. The difference is too slight to warrant forking the file into different versions. Instead, you can just check if the function exists before calling it to avoid the error:
JS
if (typeof yourFunctionName == 'function') {
yourFunctionName();
}
Comments
Great snippet! I keep this in my list of common functions and it’s saved me a million times during development.
I like to add an ‘else’ statement with an alert (or console.log()) to make the failure more visible.
if (typeof yourFunctionName == 'function') {
yourFunctionName();
}else{
alert('Check yourFunctionName!');
}
JS
if (typeof yourFunctionName == 'function') {
yourFunctionName();
}
Comments
Great snippet! I keep this in my list of common functions and it’s saved me a million times during development.
I like to add an ‘else’ statement with an alert (or console.log()) to make the failure more visible.
if (typeof yourFunctionName == 'function') {
yourFunctionName();
}else{
alert('Check yourFunctionName!');
}
Harshi124:
bye
Similar questions