Internet Explorer 8 prototypes and XMLHttpRequest

2019-03-31 11:38发布

问题:

This is partially a request for a workaround, and partially an attempt to get the word out that Internet Explorer's prototype implementation is still faulty.

The following code does not work on Internet Explorer.

XMLHttpRequest.prototype.old = XMLHttpRequest.prototype.open;
var x = new XMLHttpRequest();
x.old("POST", "test", false);

For IE 8 beta, and all previous versions, the XMLHttpRequest.prototype property never existed in the first place. In IE8, it does exist, but you'll get a "Invalid procedure call or argument" error. Internet Explorer doesn't like decoration.

Does anyone know of a workaround for this?

Update:

It has been pointed that I could override the entirety of XMLHttpRequest with a new function and constructor, and then create a wrapper script ala XMLHttpRequest.js. The prototype method is much shorter, so I would still prefer to use it for non-IE browsers.

回答1:

The problem seems to be that IE 8 recognizes XMLHttpRequest, but not as a function. Active X objects still seem to work. Instead of testing for the existance of window.XMLHtppRequest, I test for the typeof window.XMLHtppRequest. seems to work OK.

I recoded my get request as follows:

FG.ajax.getxhr = function(){
var xhr;
if (typeof window.XMLHttpRequest === 'function') {
    xhr  = XMLHttpRequest();
}
else {
    xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
return xhr;