Location, Location, Location

Why where you put your script element matters

Everyone knows you add JavaScript to your page by putting your <script> element at the top of your HTML page, right? Not so fast. In part two of Head First JavaScript Programming Teasers, Eric explains the nuts and bolts of the <script> element: how to add it to your page, and where.

While you can put a <script> element just about anywhere in your code, there are a couple of things you should know before you make any decisions about where to add it. For instance, you might already know that the browser reads your page top down and starts executing your JavaScript as it gets to the code. That means if you put your JavaScript in the <head> of your document, the browser will execute the code before it loads the rest of the page. That might be what you want… or it might mean that users are sitting there looking at a blank page while your script is executing.

Watch the video for a couple of other tips about the <script> element, taken from our upcoming book, Head First JavaScript Programming.

And if you missed the first part of this video series, you can watch it here.

Related

Sign up for the O'Reilly Programming Newsletter to get weekly insight from industry insiders.