Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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> 
like image 425
Dmitry Avatar asked Feb 12 '13 17:02

Dmitry


People also ask

Does JavaScript need to be in head?

JavaScript in body or head: Scripts can be placed inside the body or the head section of an HTML page or inside both head and body.

Why do we write JavaScript code in body and head tag?

Javascript in the body is executed as it is read and as the page is rendered. Javascript in the head is interpreted before anything is rendered.

Do script tags need to be in the head?

In general, it does not matter. Scripts end up in the head tag mostly out of tradition.

Where should JavaScript be placed in head or body?

JavaScript in <head> or <body> You can place any number of scripts in an HTML document. Scripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.


2 Answers

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. 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

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

like image 105
Zen Avatar answered Oct 31 '22 17:10

Zen


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.

like image 31
Bredele Avatar answered Oct 31 '22 19:10

Bredele