http://css-tricks.com/snippets/javascript/check-if-function-exists-before-calling/
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:
if (typeof yourFunctionName == 'function') { yourFunctionName(); }
jQuery code snippet to check whether a function exists within the JavaScript code. This can be easily achieved by using the jQuery.isFunction() function. Useful for checking if a jQuery function exists before calling it!
<script type="text/javascript">
function somenoobfunction() {
}
// using jQuery
if ($.isFunction(window.somenoobfunction)) {
//execute it
somenoobfunction();
} else {
//doesnt exists... cry?!?
document.writeln('somenoobfunction does not exist');
}
</script>
TAGGED WITH: JQUERY CHECK FUNCTION, JQUERY CHECK IS FUNCTION EXAMPLE,JQUERY FUNCTION EXISTS, JQUERY FUNCTION EXISTS EXAMPLE, JQUERY.ISFUNCTION EXAMPLE, JQUERY.ISFUNCTION().
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:
if (typeof yourFunctionName == 'function') { yourFunctionName(); }
jQuery code snippet to check whether a function exists within the JavaScript code. This can be easily achieved by using the jQuery.isFunction() function. Useful for checking if a jQuery function exists before calling it!
<script type="text/javascript">
function somenoobfunction() {
}
// using jQuery
if ($.isFunction(window.somenoobfunction)) {
//execute it
somenoobfunction();
} else {
//doesnt exists... cry?!?
document.writeln('somenoobfunction does not exist');
}
</script>
TAGGED WITH: JQUERY CHECK FUNCTION, JQUERY CHECK IS FUNCTION EXAMPLE,JQUERY FUNCTION EXISTS, JQUERY FUNCTION EXISTS EXAMPLE, JQUERY.ISFUNCTION EXAMPLE, JQUERY.ISFUNCTION().
Comments
Post a Comment