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 trialAnwar Rizalman
Python Development Techdegree Graduate 33,620 PointsRegarding where to insert the script tag in the .html file
I've previous basic introductory video regarding javascript and also jquery, and all of them always recommended to put the script tag after the markup just before the closing body tag otherwise it won't work properly. I also just learnt that the DomContentLoaded event let's us to put the script tag in the header tag.
However in this workshop, I do not get why and how the script tag works like as it suppose to eventhough it is placed before the main body?
Thank you
1 Answer
Steven Parker
231,184 PointsIt depends on what the script does, and perhaps on browser timing. The main thing you want to avoid is running the script before the page loads if the script contains references to page elements. If the script doesn't work with the page elements, then the placement would not be sensitive.
Anwar Rizalman
Python Development Techdegree Graduate 33,620 PointsAnwar Rizalman
Python Development Techdegree Graduate 33,620 PointsI see. In this case the script files work with the class attribute and I suppose that would be ok too ?
Steven Parker
231,184 PointsSteven Parker
231,184 PointsIn this case, the scripts loaded in the
head
are generic libraries that wouldn't reference any page elements on their own. But the script that invokes the libraries is the last thing in thebody
.Anwar Rizalman
Python Development Techdegree Graduate 33,620 PointsAnwar Rizalman
Python Development Techdegree Graduate 33,620 PointsReally sorry I'm still a bit confused. If you seen the video, the scripts added are in the head. The MediaElement player was used by adding the class "mejs__player" to the audio and video element. We can see the changes when it was previewed.
I'm not good at English but when you said the script that invoke the libraries, did you meant about the script that targets the class "mejs__player" and then changing its api there? For example:
So in this case, we can't put the script before the body tag? Thankyou
Steven Parker
231,184 PointsSteven Parker
231,184 PointsThat last script is the one that references the
video
andaudio
elements on the page, and it's the last thing in thebody
. The ones that get loaded at the top are just libraries.