jQuery Check if Function Exists Example

Sam Deering

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>

Note: As of jQuery 1.3, functions provided by the browser like alert() and DOM element methods like getAttribute() are not guaranteed to be detected as functions in browsers such as Internet Explorer.

Further reading: http://api.jquery.com/jQuery.isFunction/

Win an Annual Membership to Learnable,

SitePoint's Learning Platform

No Reader comments

Comments on this post are closed.