Does the DOCTYPE declaration have to be the first tag in an HTML document? [duplicate]

StackOverflow https://stackoverflow.com/questions/69828

  •  09-06-2019
  •  | 
  •  

Question

This question already has an answer here:

Our security manager dynamically inserts a bit of javascript at the top of every html page when a page is requested by the client. It is inserted above the DOCTYPE statement. I think this might be the cause of the layout problems I am having.

Ideas anyone?

Was it helpful?

Solution

Yes, DOCTYPE must be the first data on the page: http://www.w3schools.com/tags/tag_DOCTYPE.asp

OTHER TIPS

Yes, the DOCTYPE must come first.

The definition is here: http://www.w3.org/TR/REC-html40/struct/global.html. Note that it says a document consists of three parts, and the DTD must be first.

The recommendation for HTML expresses it as an application of SGML, which requires that the DOCTYPE declaration appear before the HTML element (ignoring HTML comments). Even without the DOCTYPE, adding a SCRIPT element outside the HTML element (either before it or after it) is not valid HTML. Of course, HTML validity may not be a requirement for you, so long as it works in most browsers, and then the quirks-mode switching mentioned will get you: without the DOCTYPE, many browsers will switch to quirks mode, possibly changing the layout.

I assume the TAM script fragment is being added by some proxy or other which is not able to properly analyse the HTML structure of the page and insert the SCRIPT in the correct position in the HEAD or BODY of the document. In this case, adding to the end of the document, while not valid HTML, will work in most web browsers.

It could be the source of your problem though! Check out "quirks mode" as that depends on doctype settings. Further study : http://www.quirksmode.org/ explanation: you can toggle your browser into (mostly IE) strict standards compilant mode, and loose mode. This will greatly affect rendering. TAM's setting could have switched this on/off.

I read the w3 specs which just say that there are 3 parts to a document. The sequence is assumed and there is no explicit statement forbidding, for example, a little js snippit up front.

I understand that it is possible to configure TAM to add the js at the end of the dicument but it beats me why they put it up top if it can cause such obvious problems!

W3c (at w3.org), on a page called html5/syntax.html, says "a DOCTYPE is a required preamble" which I interpret to mean it is required and that it must come first.

It also says it must consist of the following components in this order:

  1. A string that is an ASCII case-insensitive match for the string <!DOCTYPE.
  2. One or more space characters.
  3. A string that is an ASCII case-insensitive match for the string html.
  4. Optionally, a DOCTYPE legacy string or an obsolete permitted DOCTYPE string (defined below).
  5. Zero or more space characters.
  6. A > (U+003E) character.

Yes, the doctype must be first thing in the document (except for comments). You should avoid inserting scripts before the doctype; compliant parsers are not required to accept that. (They should accept scripts appended after the rest of the document, if that is an alternative.)

From the HTML 5 specification:

8.1 Writing HTML documents

This section only applies to documents, authoring tools, and markup > generators. In particular, it does not apply to conformance checkers; > conformance checkers must use the requirements given in the next section > ("parsing HTML documents").

Documents must consist of the following parts, in the given order:

  1. Optionally, a single "BOM" (U+FEFF) character.
  2. Any number of comments and space characters.
  3. A DOCTYPE.
  4. Any number of comments and space characters.
  5. The root element, in the form of an html element.
  6. Any number of comments and space characters.
  7. The various types of content mentioned above are described in the next few sections.

From HTML 4.01 Specification:

7 The global structure of an HTML document

An HTML 4 document is composed of three parts:

  1. a line containing HTML version information,
  2. a declarative header section (delimited by the HEAD element),
  3. a body, which contains the document's actual content. The body may be implemented by the BODY element or the FRAMESET element.

[...]

White space (spaces, newlines, tabs, and comments) may appear before or after each section.

[...]

A valid HTML document declares what version of HTML is used in the document. The document type declaration names the document type definition (DTD) in use for the document (see [ISO8879]).

It’s not a tag, but yup. Mainly because that’s the only way to get Internet Explorer (pre-version 8, I think) into standards mode.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top