简体   繁体   English

为什么require.js进入head标签内部

[英]why does require.js go inside head tag

According to many sources such as Yahoo Developer Network, javascript goes at the bottom of the page so web page content will display before javascript is loaded. 根据雅虎开发者网络等许多来源,javascript位于页面底部,因此网页内容将在加载javascript之前显示。 Does putting call to require.js in the head tag will make a browser wait for the script to finish loading before displaying a page? 在head标签中调用require.js是否会使浏览器在显示页面之前等待脚本完成加载?

<head>
<title>My Sample Project</title>
<script data-main="scripts/main" src="scripts/require.js"></script>
</head> 

The answer is a bit complex; 答案有点复杂; It can go in either the <head> or <body> of your page, depending on what you're requiring with require.js . 它可以放在页面的<head><body>中,具体取决于require.js 要求 Certain operations logically need to happen before the content is loaded, but most things are happy to wait. 某些操作在逻辑上需要在加载内容之前发生,但大多数事情都乐于等待。

An example of something which needs to happen before the page loads is a css pre-processor like LESS , this obviously needs to run in the <head> (in reality this should probably be pre-compiled and served as static css, but it serves as a good example) - another example would be a JavaScript template system or some kind of client side MVC system like ember.js 在页面加载之前需要发生的事情的一个例子是像LESS这样的css预处理器,这显然需要在<head>运行(实际上这应该是预编译的并且作为静态css提供,但它服务作为一个很好的例子) - 另一个例子是JavaScript模板系统或某种客户端MVC系统,如ember.js

For basic presentational JavaScript it's usually safe to include it at the bottom of the page. 对于基本的表示JavaScript,通常可以将其包含在页面底部。

The answer is yes. 答案是肯定的。 That's the same thing for all the scripts in the head tag. 对于head标签中的所有脚本,这是相同的。 A script block parallel downloads and the browser continues rendering the page once it's finish being executed. 脚本块并行下载,浏览器在完成页面执行后继续呈现页面。

It depends on the content of the javascript file, when it is going to be executed. 它取决于javascript文件的内容,何时将被执行。

eg 例如

<script>
  function load() {
    alert("load event!");
  }
  window.onload = load;
</script>

the message box will display after the page is loaded. 加载页面后将显示消息框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM