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 jQuery Basics (2014) Introduction to jQuery Ways to Include jQuery in a Project

Get confused about example of adding script at bottom of the page

Hi guys,

I was used to learn from Dave and I don't like so much the way of teaching of Andrew. In particular it's not clear why at minute 7.27 he speaks about an example where we didn't wrap our jquery code in the function. What example is he speaking about??? Why he says we included the code at the bottom of the page if, till that moment, he didn't speak about adding the script at the bottom of the page??? I understood that it's better to add the script at the bottom of the page but he's not clear to show the difference about the 2 situations! I don't understand the connection of his dialogue about this example!

Can someone clarify me what he wants to tell us??

Thanks!

4 Answers

Alexander Acker
Alexander Acker
18,123 Points

Hey Luca. I believe Andrew is just trying to explain how it's possible to place your script tag either at the top of the document or at the bottom and how to get it to run if this your preference. Earlier in the video he write an expression function that doesn't work due to being placed at the top of the document before the body and DOM loads ("myCode") At 7:27 I believe that this is what he's referencing and in order to get this kind of function to work, you must place the file at the bottom of the page. The the other topics of implementing a document.ready method or passing your code within an anonymous jQuery function are ways to get this to pass if placed at the top.

Hi Alexander!

But if you look better, the other solutions Andrew shows us before in the video are working too!

From this point my troubles starts! Because he didn't show us the difference between the 2 ways of insert the script. And which is the example he's speaking about, I mean when he says there was an example where he did'nt wrap the jquery code in the function???

In fact, if you see the example at min 5:40, 6:03, 6:40 and even at 7:24. So what is the difference?

Gregory Ledger
Gregory Ledger
6,116 Points

What I'm seeing in my browser is that the p.warning element appears before the script runs, so that you see the p element, then it disappears when the hide() method is called, then shows when the show()method is called. My experience if obviously different from the video, but the script works correctly if I move it to the head element with a ready(method) called.

What gives? It seems the rest of the world also opts for putting the script at the bottom, but in my case, the dom is loading before javascript can affect it.