IE, XML, Objects and this

A

Andrew Poulos

I'm trying to use the following code to load xml files:

ImportXML = function (ts) {
this.file = ts;
if (document.implementation &&
document.implementation.createDocument) {
this.doc = document.implementation.createDocument("", "", null);
this.doc.obj = this;
this.doc.onload = this.callBack;
this.doc.load(this.file);
} else if (window.ActiveXObject) {
this.doc = new ActiveXObject("Microsoft.XMLDOM");
this.doc.onreadystatechange = this.ready;
this.doc.obj = this;
this.doc.load(this.file);
} else {
alert("Error");
}
}
ImportXML.prototype.ready = function () {
if (myDoc[counter].readyState == 4) this.obj.callBack();
}
ImportXML.prototype.callBack = function () {
alert('loaded');
}

var xmlDoc = new ImportXML("bar.xml");


The problem I'm having is with the second line that reads
this.doc.obj = this;
IE tells me that object doesn't support this property or method. How is
it possible to get a reference to the object (xmlDoc) in the prototypes
'ready' and 'callBack" with IE?

Andrew Poulos
 
T

Thomas 'PointedEars' Lahn

Andrew said:
ImportXML = function (ts) {

Use

function ImportXML(ts)
{

instead.
this.file = ts;
if (document.implementation &&
document.implementation.createDocument) {
this.doc = document.implementation.createDocument("", "", null);

The feature test is not sufficient (and the indentation
clearly allows for improvement). You should ensure that
document.implementation.createDocument is something that
can be called.

function isMethodType(s)
{
return (s == "function" || s == "object");
}

if (... && isMethodType(typeof document.implementation.createDocument))
{
this.doc = document.implementation.createDocument("", "", null);

You should also test whether

var impl;
if ((impl = document.implementation)
&& isMethodType(typeof impl.createDocument))
{
this.doc = impl.createDocument("", "", null);

works instead; saves a few lookups.
this.doc.obj = this;
this.doc.onload = this.callBack;
this.doc.load(this.file);
} else if (window.ActiveXObject) {
this.doc = new ActiveXObject("Microsoft.XMLDOM");
this.doc.onreadystatechange = this.ready;
this.doc.obj = this;
this.doc.load(this.file);
} else {
alert("Error");
}
}
ImportXML.prototype.ready = function () {
if (myDoc[counter].readyState == 4) this.obj.callBack();
}
ImportXML.prototype.callBack = function () {
alert('loaded');
}

var xmlDoc = new ImportXML("bar.xml");


The problem I'm having is with the second line that reads
this.doc.obj = this;
IE tells me that object doesn't support this property or method. How is
it possible to get a reference to the object (xmlDoc) in the prototypes
'ready' and 'callBack"

Those are not prototypes but methods of a prototype object, or short
"prototype methods".

It is not an IE-specific problem. You are creating a host object,
and host objects need not to allow the addition of new properties
(ECMAScript 3, subsections 4.3.8 and 8.6.2).

However, adding new properties is unnecessary here:

ImportXML.prototype.ready = function ()
{
if (myDoc[counter].readyState == 4)
{
this.callBack();
}
};

`this' is a reference to the calling object here.

What are `myDoc' and `counter' anyway? You should avoid
to refer to globally declared variables in methods directly.


HTH

PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
Andrew said:
this.doc = new ActiveXObject("Microsoft.XMLDOM");
[...]
this.doc.obj = this; ^^^^^^^^^^^^
this.doc.load(this.file);
} else {
alert("Error");
}
}
ImportXML.prototype.ready = function () {
if (myDoc[counter].readyState == 4) this.obj.callBack(); ^^^^^^^^
}
[...]
It is not an IE-specific problem. You are creating a host object,
and host objects need not to allow the addition of new properties
(ECMAScript 3, subsections 4.3.8 and 8.6.2). [...]

Whereas I want to add this is not going to work even if the addition of new
properties would be possible. The latter assignment operation will most
certainly result in a ReferenceError, because the calling object had no
`obj' property, but the object referred to by the `doc' property of the
calling object had.


PointedEars
 
A

Andrew Poulos

[snip]

Thanks for the info on createDocument.


It is not an IE-specific problem. You are creating a host object,
and host objects need not to allow the addition of new properties
(ECMAScript 3, subsections 4.3.8 and 8.6.2).

However, adding new properties is unnecessary here:

ImportXML.prototype.ready = function ()
{
if (myDoc[counter].readyState == 4)
{
this.callBack();
}
};

`this' is a reference to the calling object here.

What are `myDoc' and `counter' anyway? You should avoid
to refer to globally declared variables in methods directly.

Oops, it should've read more like:

ImportXML.prototype.ready = function () {
alert(this);
if (this.doc.readystate == 4) this.callBack();
}

var xmlDoc = new ImportXML("bar.xml");

The line "alert(this);" displays [object] (which I guess is the XML
being loaded) and not [object Object] (which I guess is a reference to
xmlDoc). At any rate IE gives me the error that 'this.doc.readyState' is
null or not an object. How can you reference xmlDoc within the prototype
method 'ready'?

Andrew Poulos
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top