Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript 'Node' undefined in IE8 and under

I have the following code which seems to work fine in all browsers except IE8 and below.

$("table.availability").each( function() {
    var siteName = $(this).parent().siblings("h2").contents().filter(function() { return this.nodeType == Node.TEXT_NODE; }).text()
    alert(sitename);
});

It gets the content of an element and strips out everything contained within a child element just leaving the text of that element.

The error I get says that Node is undefined - so I declare it at the top of the js file and then get the same message about TEXT_NODE so I declare that. I then get the following error:

Unable to get property 'TEXT_NODE' of undefined or null reference

Can anyone help me to fix this or can anyone think of a better way to get the same result. Thanks.

like image 240
Tom Avatar asked Sep 01 '26 00:09

Tom


2 Answers

The TEXT_NODE constant has a value of 3. You can just use that:

return this.nodeType === 3;

Older versions of IE just don't implement the Node interface, but they do still follow the DOM spec and assign the correct nodeType property values.

If you want to use the "constant", you can declare a Node object yourself:

var Node = Node || {
    ELEMENT_NODE: 1,
    ATTRIBUTE_NODE: 2,
    TEXT_NODE: 3
    // etc... if you might need other node types
};
like image 171
James Allardice Avatar answered Sep 03 '26 13:09

James Allardice


For IE8 and below version node does not work, changing node to window worked for me.

like image 44
priyanka_rao Avatar answered Sep 03 '26 13:09

priyanka_rao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!