How to extend IXMLDOMElement using 'prototype'?
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 开发者_Go百科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?
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
精彩评论