Why does the .tagName DOM property return an uppercase value?
Asked Answered
W

1

19

For example, if we have

<html>
    <head>
        <title>FooBar</title>
    </head>
    <body></body>
</html>

If we do document.getElementByTagName("title").TagName, then we will have TITLE (uppercase). While the html standards recommends writing html tags in lowercase.

I know there is no relationship between both, but this still doesn't make sense.

Is there any reason that DOM should return tag names in uppercase?

Woolsack answered 4/8, 2012 at 19:31 Comment(2)
Can you cite the HTML standard that recommends lowercase?Feudalism
Oh I just realized that xhtml is the standard that requires tags to be lowercase, however this doesn't make difference I believe.Woolsack
A
27

Technically, this is mandated in DOM Level 1:

The HTML DOM returns the tagName of an HTML element in the canonical uppercase form, regardless of the case in the source HTML document.

The convention of uppercase tag names probably stems from legacy, when HTML was previously developed based on SGML, and element types were declared in uppercase. See this section of the HTML 4.01 spec discussing SGML, HTML and its syntax, as well as for example the HTML 4.01 Strict doctype definition. Any DOM implementations supporting HTML would follow suit.

Note that lowercase tag names are only explicitly required in XHTML (but not XML), and authors are generally recommended to write lowercase tags for easy porting between HTML/XHTML, as well as improving readability. However, this recommendation doesn't occur in the spec; all it says is that tag names are case-insensitive only in HTML as opposed to XHTML and XML.

Ardyth answered 4/8, 2012 at 19:46 Comment(2)
Are there any know instances of the main browsers (or versions of them) not respecting this rule and returning lowercase values on a call to nodeName, either as a result of a bug or otherwise?Schaffer
Not that I know of, personally.Ardyth

© 2022 - 2024 — McMap. All rights reserved.