简体   繁体   English

XHR2 addEventListener与属性

[英]XHR2 addEventListener vs property

var _xhr2 = new XMLHttpRequest();

_xhr2.upload.addEventListener('progress', function(e){ //#1
   console.log('progress');       
}, false);

_xhr2.upload.onprogress = function(e){ //#2
    console.log('progress'); 
};

_xhr2.open('POST', '/fileupload');
_xhr2.send(formData);

Could someone explain the difference between #1 and #2 above. 有人可以解释上面#1#2之间的区别。 Which one is preferred over the other? 哪一个比另一个更好? Because both seems to work. 因为两者似乎都起作用。

The reason that I'm asking is because I'm playing around a bit with the HTML5 filereader + XHR2 upload, and in the MDN examples instances of FileReader() uses #2 , while XMLHttpRequest() uses #1 . 我问的原因是因为我在处理HTML5 filereader + XHR2上传,并且在MDN示例中, FileReader()实例使用#2 ,而XMLHttpRequest()实例使用#1

Well, declaring onprogress property will overwrite the previous handlers bound to it, that's obvious and that's not the issue in you case because you're bounding it only to the new object instance. 好吧,声明onprogress属性将覆盖绑定到它的以前的处理程序,这很明显,这不是问题,因为您仅将其绑定到新的对象实例。

I think the only difference is IE compatibility. 我认为唯一的区别是IE兼容性。 addEventListener is supported for IE9+ while onprogress property (as well as other on... properties) is supported for IE5.5+. IE9 +支持addEventListener ,而IE5.5 +支持onprogress属性(以及其他on...属性)。

Note : 注意事项
Older versions of IE than 9 use attachEvent instead of addEventListener . IE 9之前的版本使用的是attachEvent而不是addEventListener

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM