Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript AJAX Basics (retiring) jQuery and AJAX The Office Status Project Revisited

J Scott Erickson
J Scott Erickson
11,883 Points

Script Includes

Probably a minor detail with a simple answer.

In all of the videos and workspaces so far in the AJAX course (thanks for making it btw!), the script includes are at the top of the index.html files.

However, I was led to believe that the most common practice (according to the JavaScript and JQuery courses, thanks again!) to include the script tag and thus the files at the end of the html. This keeps you from having to check document.ready and helps avoid loading conflicts.

In my head it sort of makes sense to say that you could include JQuery at the top and then your own scripts at the bottom and this would avoid you using JQuery in your scripts before the rest of the document has loaded....

Anywho, any guidance on this would be appreciated.

J Scott Erickson
J Scott Erickson
11,883 Points

He sort of addresses it in a later video, saying that you can really do either...

I'm still a bit curious to know what is ACTUALLY, considered best practice.

1 Answer

Aaron Graham
Aaron Graham
18,033 Points

There are a lot of opinions about this. I believe that another reason for including them at the bottom of the body, is that fetching the script file from the server is not asynchronous, meaning that if you have a large script file, it will have to download before your body loads. Putting them at the bottom of the body allows the body to load before fetching the script files from the server. This might help you out.

Dave McFarland
Dave McFarland
Treehouse Teacher

Having the body load BEFORE the body has some upsides and some downsides. If you're using jQuery to make serious user interface changes to a page -- for example turn an unordered list and a bunch of divs into a tabbed panel, hiding content that should only appear after user interaction and so on, you probably don't want the body of the page to render BEFORE the JavaScript -- because the JavaScript is transforming the presentation of the HTML, you don't want to see the body in one way, then load the JavaScript, then see the body in another format.

It really depends on the situation.

J Scott Erickson
J Scott Erickson
11,883 Points

Thanks! I'll have to do some experimenting with loading both ways and manipulating the DOM with jQuery before a load.