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/

Get your free chapter of Level Up Your Web Apps with Go

Get a free chapter of Level Up Your Web Apps with Go, plus updates and exclusive offers from SitePoint.

No Reader comments