IE8兼容模式|类型不匹配| detachEvent

时间:2015-08-12 16:43:57

标签: javascript

DOX.js

function DOX_(win, doc){
    var that = this;
    this.events = {};
    this.onload = function(func){
        var prev_onload = win.onload;
        this.addEvent(win, doc, 'load',
            function(win_, event_){
                if(prev_onload)
                    prev_onload();
                func(win_);
                that.removeEvent(win_,event_);
            }
        );
    }
    this.addEvent = function(win, doc, event_, func){
        if(win.addEventListener || doc.addEventListener){
            var compat_event = win.addEventListener || doc.addEventListener;
            if(event_ in this.events){
                compat_event(event_, function(){
                    func(win, event_);
                    this.events[event_](win, event_);
                }, true);
            }else{
                compat_event(event_, function(){
                    func(win, event_);
                }, true);
            }
            this.events[event_] = func;
        }else if(win.attachEvent || doc.attachEvent){
            var compat_event = win.attachEvent || doc.attachEvent;
            var ie_event = 'on' + event_;
            if(ie_event in this.events){
                compat_event(ie_event, function(){
                    func(win, ie_event);
                    this.events[ie_event](win, ie_event);
                });
            }else{
                compat_event(ie_event, function(){
                    func(win, ie_event);
                });
            }
            this.events[ie_event] = func;
        }
    }
    this.removeEvent = function(win, doc, event_){
        if(win.removeEventListener || doc.removeEventListener){
            var compat_event = win.removeEventListener || doc.removeEventListener;
            compat_event(event_, this.events[event_], true);
            this.events[event_] = null;
            delete this.events[event_];
        }else if(win.detachEvent || doc.detachEvent){
            var compat_event = win.detachEvent || doc.detachEvent;
            var ie_event = 'on' + event_;
            compat_event(ie_event, this.events[ie_event]);//type mismatch error
            this.events[event_] = null;
            delete this.events[ie_event];
        }
    }
}

初​​始化:

var DOX = new DOX_(window, document);
DOX.onload(function(){
    alert("hello world");
});

我正在为:

创建跨浏览器兼容性框架
  • IE7 +
  • FireFox 38+
  • Chrome 43 +
  • Netscape 7 +
  • Opera 30 +
  • Safari 5.1.7 +

IE8兼容模式(IE7)错误:

  

网页错误详情

     

用户代理:Mozilla / 4.0(兼容; MSIE 8.0; Windows NT 6.1; WOW64;   三叉戟/ 4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR   3.0.30729)时间戳:2015年8月12日星期三16:41:11 UTC

Message: Type mismatch.

Line: 68
Char: 4
Code: 0
URI: http://nportal.rrh.com/JS/DOX.js

1 个答案:

答案 0 :(得分:1)

根据IE7文档,当一个或多个参数未定义时,attachEvent会触发异常“类型不匹配”。

this.events[ie_event]返回undefined。

-Credits to Nallar