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 trialLouis Rosenstein
6,343 PointsIs there a benefit to linking your scripts.js file inside of a div within the body?
I'm just beginning, but I was wondering if there is any particular reason for, or benefit to, linking your scripts.js
file inside of a div
within your body
tags, as opposed to linking it on it's own just before the closing </body>
tag. I notice in the video that Dave has his linked inside of the "container" div
. Is there any specific reason for that?
2 Answers
Dennis Le
12,872 PointsGreat Question. The instructor did code the scripts within the div tag; mainly for demonstration purposes. Mainly to illustrate document.write()
. I believe dave did mention to put <script>
tag.... put it before the </body>
.
The are many ways on the approach on where to place the <script>
tags. In the past, for best practice is to put it before the closing </body>
. For that reason, the browser loads..... In short...it will first fetch HTML; such as index.html
. Then it will parse the HTMLβ¦.(now decide where you watch to put <script>
picture what is happen in the background)...while this is happening in the background...parser will run into a <script>
tag then fetch the external script file (lots of stuff is happening here in the background). Than the browser request the script file....stops parsing the HTML on your page f(OH nO!) . Once the script is downloaded (by than your user is now angry and left) , then it will continue to put the rest of the HTML. It is important to keep in mind about the user. Donβt make the user wait!, I believe no more than 5 seconds. To conclude, it best to put it close to the the </body>
. However, there are modern ways to put it. Such as, async and defer attributes...but as for your question. I believe it will guide you to what you want to know.
Dennis Le
12,872 PointsWelcome!
Louis Rosenstein
6,343 PointsLouis Rosenstein
6,343 PointsOk cool, and thank you for the quick response!