在HEAD标记的末尾与BODY标记之间的代码如何处理? [英] How is code *between* the end of the HEAD tag and before the BODY tag processed?

查看:93
本文介绍了在HEAD标记的末尾与BODY标记之间的代码如何处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读

我发现,在我们维护的非常复杂的代码库中,<script>标签在</head>之后和<body>之前都从外部源(javascript和jquery)以及内联脚本加载.大部分是在表单完全加载后需要加载的代码.

在我开始进行更改以将代码合并到Head标记底部之前,我想了解现在正在运行此代码的客户端的情况(目标是不会破坏当前的用户体验).这些脚本是在呈现页面之前还是之后加载?首先应该不存在的无人区号的实际状态是什么?

谢谢!

解决方案

出现在</head>结束标记和<body>起始标记之间的内容正好插入到<body>起始标记之后,除非它是不包含在其中的元素属于身体,在这种情况下,它将被插入头部的末端.

某些元素,例如script,可能出现在头部或身体中.该位置中的脚本元素将插入到头部的末尾,除非浏览器已经处于体内"插入模式,否则 会被放置在体内.

请参见 8.2.5.4.4部分插入模式 8.2. 5.4.6头后"插入模式 8.2.5.4.7 W3C HTML的正文"插入模式,以获取更多详细信息.请注意,由于历史原因,错误或故意违反该规范的决定,不能保证实现符合该规范.

I have read that it is bad to do this, and outside the spec, but I have an error that is somewhat intermittent, and may be related. Just want to know if the general consensus is "burn that code with napalm" or "it works, under these limited conditions".

I have found, in a very complex code base we maintain, <script> tags, both loading from external sources (javascript and jquery), and inline scripts, after the </head> and before the <body>. Most of it is code that needs to load after the form is fully loaded.

Before I start making changes to incorporate the code into the bottom of the Head tag, I'd like to understand what is happening right now to clients that are running this code (the goal is to not break the current user experience). Would these scripts be loaded before the page is rendered, or after? What is the actual state of this no-man's-land code that shouldn't be there in the first place?

Thanks!

解决方案

Content appearing between the </head> end tag and <body> start tag gets inserted just past the <body> start tag, unless it's an element that does not belong in the body, in which case it will be inserted at the end of the head.

Certain elements, like script, may appear in either the head or the body; script elements in that location will be inserted at the end of the head, unless the browser is already in the "in body" insertion mode, in which case they'll be placed in the body.

See sections 8.2.5.4.4 The "in head" insertion mode, 8.2.5.4.6 The "after head" insertion mode and 8.2.5.4.7 The "in body" insertion mode of W3C HTML for further details. Note that implementations are not guaranteed to follow the spec, either owing to historical reasons, bugs, or deliberate decision to violate the spec, and this behavior should not be relied on.

这篇关于在HEAD标记的末尾与BODY标记之间的代码如何处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆