在<head>或页面底部的RequireJS脚本标记?

Don*_*lor 5 javascript dependencies dom amd requirejs

RequireJS脚本标记应该位于<head>HTML页面的底部还是底部?

<script data-main="resources/js/main" src="require.js"></script>
Run Code Online (Sandbox Code Playgroud)

我见过有信誉的来源将RequireJS脚本标记放在任何一个位置,但没有信誉良好的来源似乎明确说明理想位置在哪里.

标记放在头部的参数是,在页面继续加载时,RequireJS可能已经开始异步加载和执行依赖脚本(尽管这种增益可能微不足道).

标记放置在页面底部的参数将是在模块内不需要检查准备好的DOM(但如果在加载模块的第三方系统中使用该模块,则不包括此检查可能会导致问题<head>标签中的脚本).

Sim*_*ias 2

一般最佳实践是始终将脚本放在底部。

You may have seen people including it in the HEAD as it is not doing a big difference.

I'd recommend in the bottom, because as you'll want to build your scripts before production, you'll end up loading all your script in the head otherwise.

Note that if you're running a "single page application", then it really doesn't matter as chances are your body will be empty - or almost empty.