10

I wrote this bind method and am having an issue in my preventDefault() method to work in IE. The callback line never executes. Can someone provide assistance? Thanks!

var preventDefault = function (event) {
    if (window.event) { window.event.returnValue = false; }
    else if (event.preventDefault) { event.preventDefault(); }
    else { event.returnValue = false; }
};

var bindEvent = function (ele, type, cb) {
    if (window.addEventListener) {
        ele.addEventListener(type, cb, false);
    } else if (window.attachEvent) {
        ele.attachEvent('on' + type, function () {
            event.preventDefault = function () {
                preventDefault(event);
            }.call(this);
           cb.call(ele, event);  //this does not execute
        });
    }
};
ajax333221
  • 11,436
  • 16
  • 61
  • 95
Tommy
  • 131
  • 1
  • 4
  • Remove `.call(this)` before that line that does not execute. It does not execute, because the previous throws a Syntax Error.. – Rob W May 13 '12 at 21:12

1 Answers1

11
// cancel event
function cancelEvent(event) {
   if (event.preventDefault) { 
      event.preventDefault();
   } else {
      event.returnValue = false; 
   }
}
Ram
  • 143,282
  • 16
  • 168
  • 197
  • @JeffTian You can't prevent the default action of scroll event. This is why you think it doesn't work! – Ram Nov 11 '16 at 05:26