7

"Warning: In DOM Core 1, 2 and 3, Attr inherited from Node. This is no longer the case in DOM4. In order to bring the implementation of Attr up to specification, work is underway to change it to no longer inherit from Node . You should not be using any Node properties or methods on Attr objects. Starting in Gecko 7.0 (Firefox 7.0 / Thunderbird 7.0 / SeaMonkey 2.4) , the ones that are going to be removed output warning messages to the console. You should revise your code accordingly. See Deprecated properties and methods for a complete list."

Scrolling down the page, we can see replacements for nodeName and NodeValue, using Attr.name and Attr.value.

https://developer.mozilla.org/en/DOM/Attr#Deprecated_properties_and_methods

What does it really mean for other methods like attributes or childNodes? The reference says it is deprecated but they don't give any replacement!

It is deprecated for an Attribute but is it for a Node too?

Attr object: http://www.w3schools.com/jsref/dom_obj_attr.asp

Edit: nodeValue will ONLY be deprecated for Attributes (Attr) since Attr will not inherit from a Node anymore in DOM Level 4:

Here's a quick example that helped me to understand:

<div id="myAttribute">myTextNode</div>

var myDiv = document.getElementById("myAttribute");

// If you want to get "myAttribute" from div tag

alert(myDiv.attributes[0].value);
// Correct way to get value of an attribute (displays "myAttribute")
alert(myDiv.attributes[0].nodeValue);
// Working too but deprecated method for Attr since it doesn't inherit from Node in DOM4 (.nodeValue is specific to a Node, not an Attribute)

// If you want to get "myTextNode" from div tag

alert(myDiv.childNodes[0].value);
// Not working since .value is specific to an attribute, not a Node (displays "undefined")
alert(myDiv.childNodes[0].nodeValue);
// Working, .nodeValue is the correct way to get the value of a Node, it will not be deprecated for Nodes! (displays "myTextNode")

Maybe this will avoid confusion to others when accessing Attributes/Nodes :)

2
  • i think it's safe to assume the entire Attr object will be deprecated, and i do not see any reason for using it.
    – jbabey
    Commented May 8, 2012 at 13:12
  • @jbabey Attribute object will not be deprecated and for the reason of using it I've updated my post
    – baptx
    Commented May 8, 2012 at 14:36

1 Answer 1

6

What they are saying is that objects that were Attr instances (e.g. such as those returned by Element.getAttributeNode()), used to have properties that it inherited from Node.

However, because this is not the case in DOM4, they are trying to remove this inheritance. Because of this, when you now get an instance of a Attr object, the properties listed in the deprecated list will behave as they're documented.

The big question: It is deprecated for an Attribute but is it for a Node too?: No, they are not deprecated. You can see the list of properties Node has from it's own documentation page.

Attr objects aren't used much (ever?) anyway; are you sure this concerns you?

3
  • How can we know if we are using Attr instead of Node? When I'm using document.getElementsByTagName("script")[2].attributes[0].nodeValue Firebug console says "Use of attributes' nodeValue attribute is deprecated. Use value instead." But you say nodeValue is deprecated for Attr only, that's why I'm asking.
    – baptx
    Commented May 8, 2012 at 13:30
  • 1
    It's an Attr object that I'm accessing w3schools.com/jsref/prop_attr_name.asp that's why it asks for .value instead of .nodeValue I think
    – baptx
    Commented May 8, 2012 at 13:47
  • 1
    Thanks @Matt I've understand your answer better after re-reading
    – baptx
    Commented May 8, 2012 at 14:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.