DOCTYPE声明是否必须是HTML文档中的第一个标签?
当客户端请求页面时,我们的安全管理器会在每个html页面的顶部动态插入一些javascript。它被插入到DOCTYPE语句的上方。我认为这可能是我遇到布局问题的原因。
有任何想法吗?
解决方案
回答
是的,DOCTYPE必须排在最前面。
定义在这里:http://www.w3.org/TR/REC-html40/struct/global.html。请注意,它说文档由三部分组成,并且DTD必须位于第一位。
回答
是的,DOCTYPE必须是页面上的第一数据:http://www.w3schools.com/tags/tag_DOCTYPE.asp
回答
它可能是我们问题的根源!
签出"怪异模式",因为这取决于文档类型设置。
进一步的研究:http://www.quirksmode.org/
说明:我们可以将浏览器切换为(主要是IE)严格标准的编译模式和宽松模式。这将极大地影响渲染。 TAM的设置可能已将其打开/关闭。
回答
是的,文档类型必须是文档中的第一位(注释除外)。我们应该避免在doctype之前插入脚本;兼容的解析器不需要接受它。 (如果可以的话,他们应该接受在文档其余部分之后添加的脚本。)
根据HTML 5规范:
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: Optionally, a single "BOM" (U+FEFF) character. Any number of comments and space characters. A DOCTYPE. Any number of comments and space characters. The root element, in the form of an html element. Any number of comments and space characters. The various types of content mentioned above are described in the next few sections.
从HTML 4.01规范开始:
7 The global structure of an HTML document An HTML 4 document is composed of three parts: a line containing HTML version information, a declarative header section (delimited by the HEAD element), 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]).
回答
我阅读了w3规范,该规范仅说文档包含3个部分。该序列是假定的,没有明确的禁止语句,例如,前面有一个js片段。
我知道可以将TAM配置为在文档末尾添加js,但令我不解的是,如果这会导致明显的问题,他们会把它放在首位!
回答
针对HTML的建议将其表示为SGML的应用,要求DOCTYPE声明出现在HTML元素之前(忽略HTML注释)。即使没有DOCTYPE
,在HTML
元素之外(在元素之前或者之后)添加SCRIPT
元素也不是有效的HTML。当然,只要我们在大多数浏览器中都可以使用HTML有效性,就不会要求HTML有效性,然后提到的怪癖模式切换将为我们提供:没有DOCTYPE的情况下,许多浏览器都会切换到怪癖模式,并且可能会更改布局。
我假设TAM脚本片段是由某些代理或者其他代理添加的,这些代理无法正确分析页面的HTML结构,并将" SCRIPT"插入文档的" HEAD"或者" BODY"的正确位置。在这种情况下,在文档末尾添加无效的HTML即可在大多数Web浏览器中使用。
回答
它不是标签,而是标签。主要是因为那是将Internet Explorer(我认为是第8版之前)进入标准模式的唯一方法。