<p>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.</p>

<p>Ideas anyone?</p>

[{'answer_id': 69831, 'author': 'Brad Wilson', 'author_id': 1554, 'author_profile': 'https://Stackoverflow.com/users/1554', 'pm_score': 4, 'selected': False, 'text': '<p>Yes, the DOCTYPE must come first.</p>\n\n<p>The definition is here: <a href="http://www.w3.org/TR/REC-html40/struct/global.html" rel="noreferrer">http://www.w3.org/TR/REC-html40/struct/global.html</a>. Note that it says a document consists of three parts, and the DTD must be first.</p>\n'}, {'answer_id': 69842, 'author': 'erlando', 'author_id': 4192, 'author_profile': 'https://Stackoverflow.com/users/4192', 'pm_score': 4, 'selected': True, 'text': '<p>Yes, DOCTYPE must be the first data on the page: <a href="http://www.w3schools.com/tags/tag_DOCTYPE.asp" rel="noreferrer">http://www.w3schools.com/tags/tag_DOCTYPE.asp</a></p>\n'}, {'answer_id': 69846, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>It could be the source of your problem though!\nCheck out "quirks mode" as that depends on doctype settings.\nFurther study : <a href="http://www.quirksmode.org/" rel="nofollow noreferrer">http://www.quirksmode.org/</a>\nexplanation: 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.</p>\n'}, {'answer_id': 69850, 'author': 'Jeremy', 'author_id': 1114, 'author_profile': 'https://Stackoverflow.com/users/1114', 'pm_score': 1, 'selected': False, 'text': '<p>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 <em>after</em> the rest of the document, if that is an alternative.)</p>\n\n<p>From the HTML 5 specification:</p>\n\n<blockquote>\n  <h3><a href="http://www.w3.org/TR/html5/syntax.html#writing" rel="nofollow noreferrer">8.1 Writing HTML documents</a></h3>\n  \n  <p>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").</p>\n  \n  <p>Documents must consist of the following parts, in the given order:</p>\n  \n  <ol>\n  <li>Optionally, a single "BOM" (U+FEFF) character.</li>\n  <li>Any number of comments and space characters.</li>\n  <li>A DOCTYPE.</li>\n  <li>Any number of comments and space characters.</li>\n  <li>The root element, in the form of an html element.</li>\n  <li>Any number of comments and space characters.</li>\n  <li>The various types of content mentioned above are described in the next few sections.</li>\n  </ol>\n</blockquote>\n\n<p>From HTML 4.01 Specification:</p>\n\n<blockquote>\n  <h3><a href="http://www.w3.org/TR/html401/struct/global.html" rel="nofollow noreferrer">7 The global structure of an HTML document</a></h3>\n  \n  <p>An HTML 4 document is composed of three parts:</p>\n  \n  <ol>\n  <li>a line containing HTML version information,</li>\n  <li>a declarative header section (delimited by the HEAD element),</li>\n  <li>a body, which contains the document\'s actual content. The body may be implemented by the BODY element or the FRAMESET element.</li>\n  </ol>\n  \n  <p>[...]</p>\n  \n  <p>White space (spaces, newlines, tabs, and comments) may appear before or after each section.</p>\n  \n  <p>[...]</p>\n  \n  <p>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]).</p>\n</blockquote>\n'}, {'answer_id': 69905, 'author': 'paul', 'author_id': 11249, 'author_profile': 'https://Stackoverflow.com/users/11249', 'pm_score': 1, 'selected': False, 'text': '<p>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. </p>\n\n<p>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!</p>\n'}, {'answer_id': 72063, 'author': 'pdc', 'author_id': 8925, 'author_profile': 'https://Stackoverflow.com/users/8925', 'pm_score': 2, 'selected': False, 'text': '<p>The recommendation for HTML expresses it as an application of SGML, which requires that the <code>DOCTYPE</code> declaration appear before the HTML element (ignoring HTML comments). Even without the <code>DOCTYPE</code>, adding a <code>SCRIPT</code> element outside the <code>HTML</code> 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 <code>DOCTYPE</code>, many browsers will switch to quirks mode, possibly changing the layout.</p>\n\n<p>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 <code>SCRIPT</code> in the correct position in the <code>HEAD</code> or <code>BODY</code> of the document. In this case, adding to the end of the document, while not valid HTML, will work in most web browsers.</p>\n'}, {'answer_id': 1952751, 'author': 'Paul D. Waite', 'author_id': 20578, 'author_profile': 'https://Stackoverflow.com/users/20578', 'pm_score': 0, 'selected': False, 'text': '<p>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.</p>\n'}, {'answer_id': 28060787, 'author': 'Joe Hansen', 'author_id': 756329, 'author_profile': 'https://Stackoverflow.com/users/756329', 'pm_score': 1, 'selected': False, 'text': '<p>W3c (at w3.org), on a page called <a href="http://www.w3.org/TR/html5/syntax.html#syntax-doctype" rel="nofollow">html5/syntax.html</a>, says "a <strong>DOCTYPE</strong> is a required preamble" which I interpret to mean it is required and that it must come first.</p>\n\n<p>It also says it must consist of the following components in this order:</p>\n\n<ol>\n<li>A string that is an ASCII case-insensitive match for the string <code>&lt;!DOCTYPE</code>.</li>\n<li>One or more space characters.</li>\n<li>A string that is an ASCII case-insensitive match for the string <code>html</code>.</li>\n<li>Optionally, a DOCTYPE legacy string or an obsolete permitted DOCTYPE string (defined below).</li>\n<li>Zero or more space characters.</li>\n<li>A <code>&gt;</code> (U+003E) character.</li>\n</ol>\n'}]
['https://Stackoverflow.com/questions/69828', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/11249/']