views:

17

answers:

1

I have some code, which was written couple of years ago and works only in IE. I would like, to make it work with browser now.

So, code receives XML by XHR, and then reading its content. And I have a "collection" of elements. In FF it type is Element, in IE is IXMLDOMElement. To make reading text value unified (FF uses textContent property, IE text) I want to add method like this:

Element.prototype.getText = function() {
    return this.text || this.textContent;
}

But, when I try to do this in IE with IXMLDOMElement instead of Element, then I get error (in IE), that IXMLDOMElement is not defined. How something cannot be defined, if it's that type? (checked in IE's development tools)

Can anybody give me any advice, hint how to deal with this?

A: 

The Element interface is supported in Internet Explorer from version 8. Unfortunately, interfaces are not supported in Internet Explorer before version 8.

Use the innerText property (not the text) in IE, it is supported by all HTML elements and it has the same functionality as the textContent property in FF. In JavaScript, an empty string is evaluated to false when it is used as a condition, so the (this.text || this.textContent) expression returns undefined instead of an empty string if this.text is empty and this.textContent is not supported.

Element.prototype.getText = function() { 
    return (this.textContent === undefined ? this.innerText : this.textContent); 
} 

Related links: Interfaces in JavaScript,
innerText property, textContent property

gumape
Unfortunately, I have IE8 and "Element" is not defined - checked in IE8's development console. this.innerText also doesn't worked for me - it returns undefined in IE.
singles
I think your IE8 browser is in IE7 mode (press F12 to change it)
gumape
Element interface worked for me, when I changed it to IE8 (both browser mode and document mode to IE8). Thanks for your help!
singles