0

my question is NOT about the difference between putting actual javascript/jQuery-code in <head> or <body>-section. I want to load the jQuery-lib like this:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

and I wonder whether there is a difference between doing so in the <head>-area or before end of <body>? I prefer to do it in the <head> since this way I find it easier to keep track of everything thats going on

Philli
  • 23
  • 4
  • You're saying that your question is NOT about putting JS code in `` or `` and then you're asking where to put it. So what IS your question all about? – Alon Eitan Mar 27 '19 at 08:45
  • 1
    As I said, it is about loading the jQuery library – Philli Mar 27 '19 at 09:45
  • And jQuery is not a js code? Nevermind... – Alon Eitan Mar 27 '19 at 09:57
  • Don't be so petty.. Of course it is. But I wanted to make sure I'm not talking about actual functionality but rather about implementing the library. How could you not understand? – Philli Mar 27 '19 at 10:08
  • Because there are better ways to [load](https://www.growingwiththeweb.com/2014/02/async-vs-defer-attributes.html) it and the accepted answer doesn't even mention how to implement it using the current best practices – Alon Eitan Mar 27 '19 at 10:11
  • Possible duplicate of [JavaScript - head, body or jQuery?](https://stackoverflow.com/questions/10994335/javascript-head-body-or-jquery) – Risadinha Mar 27 '19 at 10:32

1 Answers1

1

Loading the library in the <head> will ensure it is loaded before the rest of the page loads, meaning it is available for any jQuery/JS code you write or load in further down the page. This will slow down your page's initial load by however long it takes the library to be loaded.

Loading the library at the end of the <body> will mean the HTML and CSS will load first - so the page will seem to load faster, but no code that relies on the jQuery library will work until the library has loaded. Additionally, you would have to put any code that uses jQuery after the point at the end of the <body> where you load it.

Orchis
  • 296
  • 5
  • 18