JavaScript JavaScript Basics (Retired) Introducing JavaScript Review Adding Scripts and Simple JavaScript Commands

Joe Beard
Joe Beard
492 Points

I've reached this question about putting in JavaScript before the closing </body> tag. What is the answer?

Is it that one about the visitors being able to see the code before it runs? Could you tell me why the right answer is right?

3 Answers

Dale Severude
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Dale Severude
Full Stack JavaScript Techdegree Graduate 71,296 Points

When a webpage loads, it renders the html code in the browser and styles the html code with css. This is the part of the page you see. You can not see JavaScript, but it will run in the background adding functionality to the page. It takes some amount of time for the browser load the html, css, and JavaScript. Therefore we want to load the html and css first, so we can see the page. Then the JavaScript loads in the background while we don't notice it. If you load the JavaScript first, which you can do if you choose to, it might cause a delay for the user from being able to view the page. It would be an annoying user experience and poor web site performance.

Erik Robles
PRO
Erik Robles
Pro Student 10,635 Points

Of course this is becoming less and less of an issue as there has been more of an effort to standardize. There are also ways in Javascript to handle this as well.

Joe Beard
Joe Beard
492 Points

Your answers were both very helpful. Thanks so much!