3 ms·
Ask HN: HTML/JS Standards Question
Nearly everyone (except maybe my coworkers) knows that web standards dictate that lower case HTML tags must always be used. But the question is, why does the JavaScript method .tagName return the object's tag in UPPERCASE?
Good Code:
<div id="welcome">Hello</div>
Bad Code:
<DIV ID="welcome">Hello</DIV>
JavaScript:
document.getElementById("welcome").tagName;
For both, the code would return "DIV" and not "div".
- jeff18 17y agoHTML is not necessarily case sensitive, you can use upper or lowercase tags in HTML 5, and perhaps others. With that in mind, the tags should be normalized when querying what an element is (hr == HR == hR == Hr). Uppercase was chosen: http://www.whatwg.org/specs/web-apps/current-work/multipage/apis-in-html-documents.html#apis-in-html-documents http://www.whatwg.org/specs/web-apps/current-work/multipage/... "These attributes must return element names converted to ASCII uppercase, regardless of the case with which they were created."
- taitems 17y agoWhile it doesn't break a page visually, uppercase HTML tags restrict a page from validating properly?
- jeff18 17y ago"HTML" in this context is meaningless. Are you talking about XHTML, HTML4, HTML5 or what? ;) It depends on the doctype and namespace. In XHTML, an uppercase HR is technically meaningless, since it is case sensitive. In HTML5, it is not case sensitive so hR should be valid, according to the spec. Citation: "In the HTML syntax, tag names, even those for foreign elements, may be written with any mix of lower- and uppercase letters that, when converted to all-lowercase, matches the element's tag name; tag names are case-insensitive."
- Daniel_Newby 17y ago.tagName evaluates to the name of the HTML 4 element, and names are written all-caps in the HTML 4 and older specifications. All-caps is convenient because you can write about, for example, the A element by just typing its name. The standards committees renamed them to lowercase for XHTML and HTML 5, because writing about 'a' elements is ever so much more consistent. Morons. P.S. In HTML 4 and older, tags are case insensitive. And practical browsers will accept both cases for the foreseeable future.