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 trialJennifer Wong
1,622 PointsBoth script alert run before the html renders the h1 tags. Is this because of Chrome?
This is my code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <link rel="stylesheet" href="css/main.css"> <title>JavaScript Basics</title> <script> alert("Alert2"); </script> </head> <body> <div class="container"> <h1>Where to place your JavaScript code.</h1> </div> <script src="script.js"></script> </body> </html>
I get both alerts before the page renders the H1 tags, why is this happening?
3 Answers
Steven Parker
231,275 PointsIt's not just Chrome. Most modern browsers now wait until the script finishes before rendering the page. This is a change in the common behavior in the last few years.
Jaspal Singh
13,525 PointsYour Writing the script in the head of the file, you need to delete or copy that and write it into your javascript file(script.js) which you have already placed before the body tag so that it can be executed after the HTML code.
Thanks
Steven Parker
231,275 PointsI see, you're talking about the other script in the head. But it makes no difference where you put it, all the scripts will run before the page is rendered to the screen.
Jaspal Singh
13,525 Pointshi Wong you need to write your javascript code after the html code so that browser can read your html code first and run the javascript after that, for this put your script in other file and attach it to the end tag of body.
Steven Parker
231,275 PointsI'm not sure what you mean by "attach it to the end tag of body", but the code shown above has the script tags in the proper place.
Jennifer Wong
1,622 PointsJennifer Wong
1,622 PointsMakes sense, thanks!
Lawrence Wachira
593 PointsLawrence Wachira
593 PointsFirefox seems to follow the correct page sequence. Probably just how chrome handles scripts.