1

Where should I put the code for CDN scripts or styles? In the head element or before the closing body tag?

2 Answers2

2

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.

Here or here is a more detailed explanation.

Community
  • 1
  • 1
bobthedeveloper
  • 3,733
  • 2
  • 15
  • 31
1

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 ;)

Community
  • 1
  • 1
erosman
  • 7,094
  • 7
  • 27
  • 46