|
From: Andrew Poulos on 3 Jan 2006 08:09 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
From: Thomas 'PointedEars' Lahn on 3 Jan 2006 08:43 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
From: Thomas 'PointedEars' Lahn on 3 Jan 2006 08:59 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
From: Andrew Poulos on 4 Jan 2006 04:19 [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
|
Pages: 1 Prev: Loading page using browser's history Next: How can I make a window Resizable? |