Skip to main content

Bug Tracker

Side navigation

#832 closed bug (fixed)

Opened January 17, 2007 02:31AM UTC

Closed January 22, 2007 02:20AM UTC

Last modified June 19, 2007 08:49AM UTC

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.

Attachments (0)
Change History (1)

Changed January 22, 2007 02:20AM UTC by john comment:1

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