Hammer.js (IE8)- Object doesn't support property or method 'addEventListener'

Kalvin Klien picture Kalvin Klien · Aug 16, 2013 · Viewed 8k times · Source

I'm using hammer.js for a touch menu for a site, and getting:

"Object doesn't support property or method 'addEventListener'" hammer.js, line 247 character 13

with IE8.

The actual code from hammer.js that is not working:

/**
 * simple addEventListener
 * @param   {HTMLElement}   element
 * @param   {String}        type
 * @param   {Function}      handler
 */
bindDom: function(element, type, handler) {
    var types = type.split(' ');
    for(var t=0; t<types.length; t++) {
        element.addEventListener(types[t], handler, false);
    }
},

Any idea how I can fix this?

Jquery used to have a similar issue: http://bugs.jquery.com/ticket/11127

Answer

Irvin Dominin picture Irvin Dominin · Aug 16, 2013

Starting from here: addEventListener not working in IE8

You can fix the code function by checking the definition of addEventListener like :

bindDom: function (element, type, handler) {
    var types = type.split(' ');
    for (var t = 0; t < types.length; t++) {
        if (!element.addEventListener) {
            element.attachEvent(types[t], handler);
        } else {
            element.addEventListener(types[t], handler, false);
        }
    }
},

if it works we can eventually pull a request to the developers.

Docs: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget.addEventListener?redirectlocale=en-US&redirectslug=DOM%2FEventTarget.addEventListener