Bug Tracker

Opened 16 years ago

Closed 16 years ago

Last modified 16 years ago

#832 closed bug (fixed)

isFunction unreliable (affects .trigger() and others)

Reported by: Dave Owned by:
Priority: major Milestone: 1.1
Component: core Version: 1.1
Keywords: Cc:
Blocked by: Blocking:


There are several function-related problems that the new isFunction doesn't catch. Here's what I remember:


  • A DOM element with tagName="object" reports typeof(el)=="function" but it's not.


  • Referencing el.getAttribute/setAttribute in a boolean context calls it rather than just checking its existence.
  • Many DOM functions return typeof(fn)=="object", including input.blur, input.focus, and form.submit.


  • NodeLists are reported as typeof=="function"; this is caught with extra code still in $() but it would not be detected elsewhere.

For example, all of these returned false in IE on a test page with a form.


As a result, none of the default actions are triggered in IE.

Fix? Something like /[native code]/.test(String(input.blur)) works if you are desperate because that string seems to be present in "object" functions when the function is converted to a string.

Change History (1)

comment:1 Changed 16 years ago by john

Milestone: 1.1
Resolution: fixed
Status: newclosed
Version: 1.1

Thanks to your pointer, I made some great progress. I now have the function working in all major browsers (IE 6, FF 2, Safari 2, Opera 9) for all of the test cases that you provided (and some more). The final code:

function isfn( fn ) {
        return !!fn && typeof fn != "string" &&
                typeof fn[0] == "undefined" && /function/i.test( fn + "" );

The test suite: http://john.jquery.com/test/fn.html

(Warning: Lots of alerts on that page.)

Note: See TracTickets for help on using tickets.