Bug Tracker

Opened 13 years ago

Closed 13 years ago

Last modified 13 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:

Description

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

Firefox:

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

IE:

  • 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.

Safari:

  • 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.

alert(jQuery.isFunction($("form")[0].getAttribute));
alert(jQuery.isFunction($("form")[0].submit));
alert(jQuery.isFunction($("select")[0].focus));
alert(jQuery.isFunction($("select")[0].blur));

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 13 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.