小编Guy*_*ypo的帖子

无法在Firefox中覆盖Element的addEventListener

我试图以addEventListener跨浏览器的方式覆盖Element对象的方法.目的是让我可以异步加载一些第三方脚本,这些脚本过早地调用这个方法.

我创建了一个在Chrome中完美运行的HTML文件,但在Firefox上我得到了以下异常:

"上WrappedNative原型对象非法操作" nsresult: "0x8057000c(NS_ERROR_XPC_BAD_OP_ON_WN_PROTO)"

如果您注释掉文件中更改INSTANCE方法的行,则它可以正常工作.但我需要在"类型"(即原型)上进行.

任何建议,将不胜感激.

谢谢,Guypo

这是我创建的文件

<html><body>
<img id="testImg" src="http://www.blaze.io/wp-content/themes/Blaze/images/header_logoB.png">
<script>
function myLog(msg) { "undefined" != typeof(console) && console.log("Log: " + msg); }
function customListener(type, event, useCapture) {
  // Register the event
  myLog('Registering event');
  this._origListener.apply(this, arguments);
}
// Also tried HTMLImageElement
Element.prototype._origListener = Element.prototype.addEventListener;
Element.prototype.addEventListener = customListener;

var img = document.getElementById("testImg");
// Uncommenting these lines works - but in the real case I can't access these objects
//img._origListener = img.addEventListener;
//img.addEventListener = customListener;
img.addEventListener('load',function() { …
Run Code Online (Sandbox Code Playgroud)

javascript firefox events prototype addeventlistener

2
推荐指数
1
解决办法
2696
查看次数