admin管理员组

文章数量:1336632

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. 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>
<title>My Sample Project</title>
<script data-main="scripts/main" src="scripts/require.js"></script>
</head> 

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. 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>
<title>My Sample Project</title>
<script data-main="scripts/main" src="scripts/require.js"></script>
</head> 
Share Improve this question edited Feb 12, 2013 at 22:23 Kijewski 26.1k14 gold badges107 silver badges147 bronze badges asked Feb 12, 2013 at 17:57 DmitryDmitry 4,37311 gold badges49 silver badges58 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

The answer is a bit plex; It can go in either the <head> or <body> of your page, depending on what you're requiring with 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-piled 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

For basic presentational JavaScript it's usually safe to include it at the bottom of the page.

The answer is yes. That's the same thing for all the scripts in the head tag. 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.

e.g.

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

the message box will display after the page is loaded.

本文标签: javascriptwhy does requirejs go inside head tagStack Overflow