tags:

views:

171

answers:

2

Hi,

I have a node that I'm note sure is an element (from calling node.previousSibling). However I am having trouble finding out the cross browser javascript way to access the Node constants shown on the MDC.

In all browsers but IE Node.ELEMENT_NODE is defined. I tried using a specific instance of node, e.g.:

e=$("#element_id")[0];
alert("ELEMENT_NODE: " + ELEMENT_NODE);

This does not work in IE either. So whats the IE way to do this? Do I just have to define the node constants myself?

+2  A: 

Internet Explorer doesn't define the node type constants so you would have to define them yourself. Additionally it only supports types 1 and 3.

Greg
Thats annoying.
Justin Dearing
A: 

The cleanest way to define the Node Constants [when they don't exist] is by catching the exception generated when attempting to access them.

try {
    if (Node.ELEMENT_NODE != 1) {
        throw true;
    }
}
catch(e) {
    var Node = {
        ELEMENT_NODE:       1,
        ATTRIBUTE_NODE:     2,
        TEXT_NODE:          3
    };
}

The throw true line never executes; it's only there to hint at what's really happening.

Parasyte