sazr
sazr

Reputation: 25928

Javascript Prototype inherits from ActiveXObject causes error in Internet Explorer

I have a Javascript object/class that inherits from the ActiveXObject. But I get this weird error when I run the code in Internet Explorer (version 8).

The error is: "Object doesn't support this property or method"

Can you tell me what the error means & how to fix this error?

My code is:

  function XMLHandler( xmlFilePath )
  {
     this.xmlDoc  = null;
     this.xmlFile = xmlFilePath;
     this.parseXMLFile( this.xmlFile );

     this.getXMLFile = function()
     {
        return this.xmlFile;
     }
  }

  XMLHandler.prototype              = new ActiveXObject("Microsoft.XMLDOM");
  XMLHandler.prototype.constructor  = ActiveXObject;          // Error occurs here in IE. The error is: "Object doesn't support this property or method"
  XMLHandler.prototype.parseXMLFile = function( xmlFilePath ) // If I comment out the above line then the exact same error occurs on this line too
  {
     this.xmlFile = xmlFilePath;
     this.async="false";  // keep synchronous for now
     this.load( this.xmlFile );
  }

Upvotes: 0

Views: 501

Answers (1)

Raynos
Raynos

Reputation: 169421

The error is pretty obvouis to me. What your doing is:

var x = new ActiveXObject("Microsoft.XMLDOM");
x.extendIt = 42;

And it throws a (cryptic) error saying you cannot extend an instance of a ActiveXObject with a new property.

Now an ActiveXObject is a host object and they are known to be full of undefined behaviour. Don't extend it. Instead use it.

var XMLHandler = {
    XMLDOM: new ActiveXObject("Microsoft.XMLDOM"),
    parseXMLFile: function(xmlFilePath) {
        this.xmlFile = xmlFilePath;
        this.XMLDOM.load(xmlFilePath);
    },
    getXMLFile: function () {
        return this.xmlFile;
    }
};

var xmlHandler = Object.create(XMLHandler);
xmlHandler.parseXMLFile(someFile);

(I fixed up your code, you will need an ES5 shim for legacy platform support).

Of course if you look at your code now, you can see that you created a proxy for .load for no reason. You might as well use the XMLDOM object directly.

Upvotes: 1

Related Questions