Monday, August 6, 2012

jquery 1.7.1 and IE8

jQuery 1.7.1 running on IE8 fails when trying to call the focus method on elements while opening jQuery UI dialog.

In  jquery-1.7.1.js, function jQuery.event.trigger (a method of jQuery.event) begins with:

    trigger: function( event, data, elem, onlyHandlers ) {

        // Don't do events on text and comment nodes

        if ( elem && (elem.nodeType === 3 || elem.nodeType === 8) ) {

            return;

        }
and ends with

            // Call a native DOM method on the target with the same name name as the event.
            // Can't use an .isFunction() check here because IE6/7 fails that test.
            // Don't do default actions on window, that's where global variables be (#6170)
            // IE<9 dies on focus/blur to hidden element (#1486)
            if ( ontype && elem[ type ] && ((type !== "focus" && type !== "blur") || event.target.offsetWidth !== 0) && !jQuery.isWindow( elem ) ) {
                // Don't re-trigger an onFOO event when we call its FOO() method
                old = elem[ ontype ];

                if ( old ) {
                    elem[ ontype ] = null;
                }

                // Prevent re-triggering of the same event, since we already bubbled it above
                jQuery.event.triggered = type;
                elem[ type ]();
                jQuery.event.triggered = undefined;

                if ( old ) {
                    elem[ ontype ] = old;
                }
            }
        }
    }
    return event.result;
},


In the last bit, change


elem[ type ]();
to
try {
     elem[ type ]();
} catch(err) {
     // ignore errors
}
 With this change the dialog opens successfully.

Labels