将脚本标签放在body结束标签 (</body>
)之后有多错误?
<html>
....
<body>
....
</body>
<script type="text/javascript" src="theJs.js"></script>
</html>
将脚本标签放在body结束标签 (</body>
)之后有多错误?
<html>
....
<body>
....
</body>
<script type="text/javascript" src="theJs.js"></script>
</html>
<script src="..." defer>
,它适用于所有主要浏览器(尽管在IE9及以下版本中可能存在潜在的错误)。 - Andy Edefer
仅适用于外部脚本文件(即您还必须指定 src
属性)。您不能将包含脚本的 <script>
元素“延迟”。 - Ian Boyd正如安迪所说,该文档将无效,但脚本仍将被解释执行。例如,可以查看WebKit中的代码片段:
void HTMLParser::processCloseTag(Token* t)
{
// Support for really broken HTML.
// we never close the body tag, since some stupid web pages close it before
// the actual end of the doc.
// let's rely on the end() call to close things.
if (t->tagName == htmlTag || t->tagName == bodyTag
|| t->tagName == commentAtom)
return;
...
自从 10 版本后,Internet Explorer不再支持此类脚本,并将忽略它们。
Firefox 和 Chrome 仍然兼容这些脚本,但未来有可能会将其标记为非标准内容而不再支持。
(body和html元素的结束标签可以省略,不会有任何问题;不管有没有空格,它们之后的内容都会被解析为body元素。)
请参考https://html.spec.whatwg.org/multipage/syntax.html。 - undefined<body>
<!-- main body content -->
<script src="scripts/main.js"></script>
</body>
<head>
<script src="scripts/main.js" defer></script>
</head>
script
标签有一个event
属性,可以定义来确定何时解析脚本。因此,您可以使用event="load" event="DOMContentLoaded"
在DOM创建后运行脚本或在窗口beforeunload
事件上使用event="beforeunload"
。例如,<script src="scripts/main.js" event="DOMContentLoaded"></script>
。 - 1.21 gigawatts是的。但如果你将代码添加到它之外,大多数浏览器可能会自动修复,但这仍然是一个不好的习惯。
您可以将其放置在下面,或者也可以放在head标签内,但通常的做法是在</body>之前命名一个注释以便以后轻松使用,并打开一个<script>将任何JS放在其中</script></body></html>。
<script>
window.addEventListener('DOMContentLoaded', event => {
// Activate Bootstrap scrollspy on the main nav element
const sideNav = document.body.querySelector('#sideNav');
if (sideNav) {
new bootstrap.ScrollSpy(document.body, {
target: '#sideNav',
offset: 74,
});
};
// Collapse responsive navbar when toggler is visible
const navbarToggler = document.body.querySelector('.navbar-toggler');
const responsiveNavItems = [].slice.call(
document.querySelectorAll('#navbarResponsive .nav-link')
);
responsiveNavItems.map(function (responsiveNavItem) {
responsiveNavItem.addEventListener('click', () => {
if (window.getComputedStyle(navbarToggler).display !== 'none') {
navbarToggler.click();
}
});
});
});
</script>
<!-- Bootstrap core JS-->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
从技术上讲,在body标签之后放置脚本标签是不正确的,因为页面内容的呈现是以body(或者是head)结束的。
但是浏览器有一定的容错性(尽管我不会把它作为普遍真理,因为你永远不知道),它们会:
为确保安全起见,您可以:
这种规范是被广泛接受的做法/约定,并且可以消除任何疑虑。
当你保持安全并做最合理的事情时,也请记住需要担心的是性能,因为内部/外部源文件的加载/下载、解析和解释取决于脚本标记的发生位置,即使你使用了defer或async。
<!-- Moved (prepend) into the head -->
<script>console.log(1);
</script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Remains where it is -->
<script>
console.log(2);
</script>
<title>Document</title>
</head>
<body>
<h1>Content goes here</h1>
<!-- Remains where it is -->
<script>
console.log(3);
</script>
<h1>Content goes here</h1>
<!-- Remains where it is -->
<script>
console.log(4);
</script>
</body>
</html>
<!-- Moved (append) into the body -->
<script>
console.log(5);
</script>
谷歌在“CSS优化”方面实际上推荐这样做。他们建议将关键的以上折叠样式内联,而将其余部分(CSS文件)延迟加载。
例如:
<html>
<head>
<style>
.blue{color:blue;}
</style>
</head>
<body>
<div class="blue">
Hello, world!
</div>
</body>
</html>
<noscript><link rel="stylesheet" href="small.css"></noscript>
请参见:优化CSS交付
body
元素之外。那篇谷歌文章并未建议任何人这样做。 - ChaseMoskal