Where should I put the code for CDN scripts or styles? In the head element or before the closing body tag?
-
Why do you think it is any different for CDNs? – Bergi Apr 20 '14 at 16:52
-
Because CDNs are faster? – Apr 20 '14 at 16:55
2 Answers
Put the css files in the head, your javascript at the bottom of the body.
This way the content will load faster, also if you put the scripts t the top it could be that your page rendering will stop at some point because you're downloading the javascript files.

- 1
- 1

- 3,733
- 2
- 15
- 31
-
-
1But, your reference says that you should put scripts in the head unless you have load issues :( – hazer_hazer Feb 14 '21 at 14:07
There are different philosophies regarding the best location for the script tag.
Here is another philosophy: Adding a script to HTML
Putting scripts in HEAD is easy and guarantees that they will be available before the page is shown.
There is also (in above document):
Scripts at the end of BODY
A script can also be at the bottom of page body. In this case it executes after the page is shown.
Good, because user doesn’t have to wait for scripts.
Bad, because the functions become available after the HTML is loaded. A user has a chance to click on button which may not work. Usually adding special code that hides functionality until the script has loaded resolves the problem.
AFA style, it works best in head
Good luck ;)