469,336 Members | 5,248 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

IE, XML, Objects and this

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
3 1316
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
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
[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.

Similar topics

9 posts views Thread by relaxedrob | last post: by
14 posts views Thread by Jess | last post: by
28 posts views Thread by walterbyrd | last post: by
reply views Thread by PopeDarren | last post: by
11 posts views Thread by =?ISO-8859-1?Q?Une_B=E9v?==?ISO-8859-1?Q?ue?= | last post: by
13 posts views Thread by DigitalDave | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by haryvincent176 | last post: by
reply views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.