How to access constants like Node in IE

I have a node, which I am sure is an element (from a call to node.previousSibling). However, I had trouble finding a cross-browser javascript method for accessing the Node constants specified in the MDC .

In all browsers, but IE node.ELEMENT_NODE is defined. I tried using a specific instance of node, for example:

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

This also does not work in IE. So what does IE have a way to do this? Should I just define node constants?

+4
source share
3 answers

Internet Explorer 8 and earlier do not define constants like node, so you have to define them yourself. In addition, Internet Explorer 7 and earlier only support types 1 and 3 .

+6
source

The cleanest way to define Node constants [when they don't exist] is to catch the exception that is thrown when you try to access them.

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

The throw true string is only executed when Node exists, but Node.ELEMENT_NODE not the expected value.

+1
source
 alert(oNode.nodeType) 

and you will receive:

 "1" for ELEMENT_NODE "2" for ATTRIBUTE_NODE "3" for TEXT_NODE "4" for CDATA_SECTION_NODE "5" for ENTITY_REFERENCE_NODE "6" for ENTITY_NODE 

etc...

0
source

All Articles