在JavaScript文档中嵌入JavaScript时,放置<script>标记和包含JavaScript 的适当位置在哪里?我似乎记得你不应该把它们放在这个<head>部分中,但放在该部分的开头<body>也是不好的,因为在完全呈现页面之前必须解析JavaScript(或类似的东西).这似乎将该部分的末尾<body>作为<script>标记的逻辑位置.
所以,在这里是把正确的地方<script>标记?
(这个问题引用了这个问题,其中建议JavaScript函数调用应该从<a>标签移动到<script>标签.我专门使用jQuery,但更一般的答案也是合适的.)
我正在创建小型角度应用程序,其中我已将 app.js 放在我的应用程序文件夹中,其中 index.html 也在那里。当我运行 'ng serve' 命令然后 index.html 正在加载但在 chrome 上执行 F12 时,它显示以下错误:GET http://localhost:4200/app.js net::ERR_ABORTED 404 (Not Found)
我的 index.html 文件:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>GridDemo</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<script src="app.js"></script>
</head>
<body>
<app-root></app-root>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
即使添加 <base href="/"></base>不会产生任何影响。