简体   繁体   English

如何检测JavaScript中的document.addEventListener支持

[英]How do I detect document.addEventListener support in javascript

I have this javascript code: 我有这个javascript代码:

   initializeEventHandlers: function() {
      if ( typeof document.implementation != "undefined" &&
         document.implementation.hasFeature("HTML",   "1.0") &&
         document.implementation.hasFeature("Events", "2.0") &&
         document.implementation.hasFeature("CSS",    "2.0") ) {
         document.addEventListener("mouseup",   this._mouseUpHandler.bindAsEventListener(this),  false);
         document.addEventListener("mousemove", this._mouseMoveHandler.bindAsEventListener(this), false);
      }
      else {
         document.attachEvent( "onmouseup",   this._mouseUpHandler.bindAsEventListener(this) );
         document.attachEvent( "onmousemove", this._mouseMoveHandler.bindAsEventListener(this) );
      }
   }

That works in most browsers, but when I use IE11 it fails. 在大多数浏览器中都可以使用,但是当我使用IE11时会失败。 I know this is because IE11 removed support for attachEvent, and IE11 falls through to the else condition. 我知道这是因为IE11删除了对attachEvent的支持,而IE11陷入了else条件。 I also see that hasFeature is deprecated, so I am not sure how best to detect addEventListner support. 我还看到hasFeature已过时,因此我不确定如何最好地检测addEventListner支持。

The following code works for IE11 and Firefox, but will it work for other/older browsers? 以下代码适用于IE11和Firefox,但是它将适用于其他/较旧的浏览器吗?

   initializeEventHandlers: function() {
      if (document.addEventListener) { 
         document.addEventListener("mouseup",   this._mouseUpHandler.bindAsEventListener(this),  false);
         document.addEventListener("mousemove", this._mouseMoveHandler.bindAsEventListener(this), false);
      }
      else if (document.attachEvent) {
         document.attachEvent( "onmouseup",   this._mouseUpHandler.bindAsEventListener(this) );
         document.attachEvent( "onmousemove", this._mouseMoveHandler.bindAsEventListener(this) );
      }
   }

The robust and safe way to see if document accepts the method is to use "try { ... } catch (err){ ... }" construction: 查看文档是否接受该方法的健壮且安全的方法是使用“ try {...} catch(err){...}”构造:

const initializeEventHandlers = () => {
  try {      
    if (document.addEventListener) { 
       document.addEventListener("mouseup",   this._mouseUpHandler.bindAsEventListener(this),  false);
       document.addEventListener("mousemove", this._mouseMoveHandler.bindAsEventListener(this), false);
    }
    else if (document.attachEvent) {
       document.attachEvent( "onmouseup",   this._mouseUpHandler.bindAsEventListener(this) );
       document.attachEvent( "onmousemove", this._mouseMoveHandler.bindAsEventListener(this) );
    }
  }
  catch(err) {
    console.info('Error', err );
  }   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何删除 document.addEventListener? - How do you remove a document.addEventListener? 钩上JavaScript“ document.addEventListener” - hook on javascript “document.addEventListener” Javascript - 使用 document.addEventListener 删除类不起作用 - Javascript - Remove class with document.addEventListener not working Document.addEventListener “this” 上下文 - Document.addEventListener “this” context 使用document.addEventListener使用JavaScript检测IE8 - Detecting IE8 with JavaScript using document.addEventListener Javascript数组使用document.addEventListener切换链接 - Javascript array to toggle link using document.addEventListener 使用javascript document.addEventListener在下一个窗口中打开标签 - Open tab in next window with javascript document.addEventListener javascript的document.addeventlistener无法在Chrome中使用“后退”按钮 - document.addeventlistener of javascript is not working for back button in chrome 如何将 document.addEventListener("scroll") 放在其他文件中以在我的组件中调用它? - how can I put a document.addEventListener("scroll") in other file to call it in my component? document.addEventListener 不在 JEST 的覆盖范围内 - document.addEventListener is not coverage in JEST
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM