jQuery Check if Function Exists Example

Sam Deering
Tweet

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/

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

No Reader comments