14

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?

1
  • 4
    If the "security manager" is a person, it should be fired. If it's a software, it should be uninstalled. Smells like horrible code to me. Apr 11, 2013 at 3:28

8 Answers 8

13

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.

7

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

3
  • 27
    Could you link to W3C as reference? W3schools isn't affiliated with W3C, isn't authoritative source on the subject, and sometimes their information is wrong (but they're OK in this case).
    – Kornel
    Oct 19, 2008 at 0:50
  • 4
    w3fools.com Please help stop this inequality in web.
    – Zerium
    Sep 10, 2012 at 7:40
  • This issue is resolved in IE10. The comments before Doctype would not force IE10 to quirks mode.
    – Annie
    Nov 16, 2012 at 21:28
2

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.

1

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.

1

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!

1
  • 1
    <script> tags aren't permitted outside of the <html> page, but if your only options are before or after you should probably choose after, as it will have fewer side effects.
    – Jeremy
    Sep 16, 2008 at 21:25
1

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.
1

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]).

0

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.

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