Search code examples
javascriptinternet-explorerdhtml

Polyfill for attachEvent removing dhtmlx attachEvent function


I`m adding chrome support for our legacy product (ie8 support), problem is that i adding polyfill for attachEvent

if (!isIE() && Object.attachEvent == null) {
    Object.defineProperty(Object.prototype, 'attachEvent', {
        value: function(event, func) {
            if ('string' !== typeof event || event.indexOf('on') !== 0) {
                return;
            }
                this.addEventListener(event.substring(2), func, false);
        },
        enumerable: false
    });
}

but we are using dhtmlx 3rd party library, that manages events with attachEvent function, so my polyfill overrides this function, what makes dhtmlx miss-function.

Any ides how can I solve this issue? I want to polyfill attachEvent but not to override attachEvent of dhtmlx Thanks!


Solution

  • No need to check for IsIE() ... since only IE uses the non-standard attachEvent anyway

    For other browsers, you could polyfill EventTarget.prototype.attachEvent - since this is where addEventListener is defined anyway

    if (EventTarget.prototype.attachEvent == null) {
        Object.defineProperty(EventTarget.prototype, 'attachEvent', {
            value: function(event, func) {
                if ('string' !== typeof event || event.indexOf('on') !== 0) {
                    return;
                }
                this.addEventListener(event.substring(2), func, false);
            },
            enumerable: false
        });
    }