the <script> tag with the jQuery library reference must be inside <head> or <body> tag?

Max picture Max · Oct 25, 2012 · Viewed 11.8k times · Source

Possible Duplicate:
Where is the best place to put <script> tags in HTML markup?

I always thought that it's better to put it in the head (maybe because it is loaded first in the head and also to group all the scripts in the head for easy reading), but I found several examples over Internet (bootstrap documentation, MVC4... etc) where the script tag with the reference to jQuery () is inserted into the body:

<!DOCTYPE html>
<html>
<head>
<title>Bootstrap 101 Template</title>
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<h1>Hello, world!</h1>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>

What's the best practice?

Answer

Quentin picture Quentin · Oct 25, 2012

the <script> tag with the jQuery library reference must be inside <head> or <body> tag?

Yes, either.

There can be performance benefits to putting it in one or the other (particularly at the end of the body) although that is a tradeoff between delaying when the JS can run and blocking the loading of other content while the JS downloads and parses.