admin管理员组

文章数量:1208155

I often hear about "DOM level 1", "DOM level 2", "DOM level 3" and "DOM level 4" and realized that I don't know the difference between any of them or how they relate to each other.

I know the very basics - DOM is Document Object Model, and is what provides access for scripting languages (particularly, but as far as I know, not limited to various versions of ECMAScript, such as ECMAScript 5.1) to access elements of an HTML document. (Some sites I read - such as the dom introduction on quirksmode - say that it's for any XML document, but HTML is a sufficient subset.)

The dates on w3c's DOM technical reports seem to imply that each subsequent DOM level supersedes the previous ones.

Sadly, the best reference I've found to provide clarification has been wikipedia, which seems to say the same - the Standardization section says subsequent levels "added" extra functionality, while not mentioning removing anything.

Now, for my questions, which may be rapid fire, but hopefully express the general state of my ignorance:

  • What's the relation of one DOM level to another?
  • Are lower level DOMs complete subsets of higher level DOMs? Has any functionality been removed as the DOM level advances? When I see statements like The level 1 DOM will work fine on an HTML document and In the Level 1 DOM, each object, whatever it may be exactly, is a Node (both from the quirksmode intro), does this imply that such statements are true for levels 2, 3 and 4? (These are all kind of the same question, just asked different ways)
  • Is citing DOM level really little more than a shorthand way of how modern a user agent must be for a particular function to work?

Obviously, I can study each specification off of the w3c's DOM technical reports, but was hoping to get answers from those with first-hand experience. Just by glancing at the changes section of the spec for DOM level 3, I see that most of the changes from 2 to 3 were additions, though some of the key implementations in the Node interface have changed. Did these changes break anything?

I'd like to do more than just nod sagely next time someone tells me, "Oh, that's DOM level 2, so it's ok," so would welcome any references I have missed or firsthand information that I didn't glean from my research.

I often hear about "DOM level 1", "DOM level 2", "DOM level 3" and "DOM level 4" and realized that I don't know the difference between any of them or how they relate to each other.

I know the very basics - DOM is Document Object Model, and is what provides access for scripting languages (particularly, but as far as I know, not limited to various versions of ECMAScript, such as ECMAScript 5.1) to access elements of an HTML document. (Some sites I read - such as the dom introduction on quirksmode - say that it's for any XML document, but HTML is a sufficient subset.)

The dates on w3c's DOM technical reports seem to imply that each subsequent DOM level supersedes the previous ones.

Sadly, the best reference I've found to provide clarification has been wikipedia, which seems to say the same - the Standardization section says subsequent levels "added" extra functionality, while not mentioning removing anything.

Now, for my questions, which may be rapid fire, but hopefully express the general state of my ignorance:

  • What's the relation of one DOM level to another?
  • Are lower level DOMs complete subsets of higher level DOMs? Has any functionality been removed as the DOM level advances? When I see statements like The level 1 DOM will work fine on an HTML document and In the Level 1 DOM, each object, whatever it may be exactly, is a Node (both from the quirksmode intro), does this imply that such statements are true for levels 2, 3 and 4? (These are all kind of the same question, just asked different ways)
  • Is citing DOM level really little more than a shorthand way of how modern a user agent must be for a particular function to work?

Obviously, I can study each specification off of the w3c's DOM technical reports, but was hoping to get answers from those with first-hand experience. Just by glancing at the changes section of the spec for DOM level 3, I see that most of the changes from 2 to 3 were additions, though some of the key implementations in the Node interface have changed. Did these changes break anything?

I'd like to do more than just nod sagely next time someone tells me, "Oh, that's DOM level 2, so it's ok," so would welcome any references I have missed or firsthand information that I didn't glean from my research.

Share Improve this question edited Dec 2, 2013 at 17:44 zero0 87712 silver badges26 bronze badges asked Dec 2, 2013 at 17:17 Scott MermelsteinScott Mermelstein 15.4k4 gold badges49 silver badges77 bronze badges 2
  • 2 Unrelated: XHTML is a subset of XML, HTML is not, though it is very similar. +1 for interesting question though, looking forward to the answers. – Tibos Commented Dec 2, 2013 at 17:28
  • Seems like the wikipedia page says it all. If each level adds extra functionality, then it doesn't implicitly remove anything. If the new functionality involves the removal of a previous feature, then it would still need to "add" that functionality (the removal or deprecation). – Blue Skies Commented Dec 2, 2013 at 17:42
Add a comment  | 

2 Answers 2

Reset to default 16

First, I'll relate a message from MDN's writeup of DOM levels (emphasis in original):

The DOM used to be written as a set of levels. That is no longer the case. These days it is maintained as the DOM Living Standard. This page provides an historical overview of the olden days.

This is confirmed in a W3C document called "W3C DOM4". We might take that to mean "DOM Level 4", and assume it adds an additional DOM level, but the text of the specification actually says:

This document is published as a snapshot of the DOM Living Specification.

So, this is a historical discussion, but still one worth having.

A "DOM Level" was a collection of specifications that described DOM objects, methods, and behaviors. Higher levels of the DOM specification built on the previous levels. Changes happened in two ways:

  1. The addition of a totally new specification category (e.g., Level 3 adds "Validation" and "Load and Save" specifications, which did not exist in Level 2)

  2. The modification of an existing specification category (e.g. updating the "Core" spec)

Obviously, the first type of change was purely additive, rather than subtractive. The second kind of change also seems to have been nearly exclusively additive, probably because the W3C was interested in preserving backward compatibility with previous versions.

Changes that are not backward-compatible tend to be rare and fairly minor. The Document.doctype change you cite, for example, was actually largely additive. Level 3 added the sentence:

For HTML documents, a DocumentType object may be returned, independently of the presence or absence of document type declaration in the HTML document.

This simply gave greater flexibility to allow DOM implementations to add in a doctype in HTML when the author omitted a <!DOCTYPE>. The only functionality this would break is the ability to programmatically detect the presence of an author-specified doctype, which doesn't seem to be particularly valuable.

Probably the reason you've heard someone say, "Oh, that's DOM level 2, so it's ok," is because DOM level 2 is more widely supported than DOM level 3. In some cases, this isn't even a question of old browser support: Firefox marked their lack of support for the DOM 3's "Load and Save" specification as WONTFIX. All Level 2 specifications, by contrast, are supported pretty well by modern browsers, and enjoy support from much older browsers (since Level 2 is four years older than level 3).

Just a couple of notes on DOM4 to add to apsillers' answer:

... In the Level 1 DOM, each object, whatever it may be exactly, is a Node ..., does this imply that such statements are true for levels 2, 3 and 4?

That's a definite no. Attributes in DOM4 are not Nodes.

DOM4 makes a number of significant non-backward compatible changes. The attributes are not nodes change is a big one if you're not using javascript or a duck typed language. Also document.createElement() on a XML document will create the element in the http://www.w3.org/1999/xhtml namespace, where earlier levels create the element in no namespace. Browsers have long done this, but typical XML oriented DOM implementations have used the DOM3 and earlier way. That's a big shift if you migrate from a DOM3 implementation to a DOM4 one in a non-browser context.

本文标签: javascriptWhat is the difference in DOM levelsand how do they interrelateStack Overflow