By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,903 Members | 1,856 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,903 IT Pros & Developers. It's quick & easy.

IE, XML, Objects and this

P: n/a
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
Jan 3 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Andrew Poulos wrote:
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".
with IE?


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
Jan 3 '06 #2

P: n/a
Thomas 'PointedEars' Lahn wrote:
Andrew Poulos wrote:
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
Jan 3 '06 #3

P: n/a
[snip]

Thanks for the info on createDocument.

with IE?

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
Jan 4 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.