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
    } else {
		//doesnt exists... cry?!?
        document.writeln('somenoobfunction does not exist');

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 JavaScript: Novice to Ninja Sample

Get a free 32-page chapter of JavaScript: Novice to Ninja and receive updates on exclusive offers from SitePoint.

No Reader comments