Start Here! Learn JavaScript - Cdn.oreilly.com
Start Here! Learn JavaScript - Cdn.oreilly.com
Start Here! Learn JavaScript - Cdn.oreilly.com
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
placing <strong>JavaScript</strong> in a Webpage<br />
The overall title for this section is “Where <strong>JavaScript</strong> Fits,” but so far I’ve discussed only the conceptual<br />
environment in which <strong>JavaScript</strong> operates. It’s time to fix that by discussing the literal location for<br />
<strong>JavaScript</strong> code on a webpage.<br />
Just as you need to use an HTML tag to inform the browser it should expect an image, you<br />
use the tag to inform the browser it will be reading a script of some sort. There are a few different<br />
kinds or types of scripts web browsers can read; <strong>JavaScript</strong> is one of them.<br />
You place <strong>JavaScript</strong> within the or section (or both) of an HTML document and<br />
you can place multiple scripts on a given page.<br />
The browser executes <strong>JavaScript</strong> as it is encountered during the page-parsing process. This has<br />
practical implications for <strong>JavaScript</strong> developers. If your <strong>JavaScript</strong> program attempts to work with<br />
some elements of the HTML document before those elements have been loaded, the program will<br />
fail. For example, if you place <strong>JavaScript</strong> in the section of a page and that code attempts to<br />
work with an HTML element that’s all the way down at the bottom of the page, the program might<br />
fail because the browser doesn’t yet have that element fully loaded. Unfortunately, the program will<br />
probably fail in subtle and difficult to troubleshoot ways; one time the program will work, and the<br />
next time it won’t. That happens because one time the browser will have loaded that HTML element<br />
by the time it executes the <strong>JavaScript</strong> code, but the next time it won’t. An even more fun (not really)<br />
failure scenario is when everything works in your local development environment on your <strong>com</strong>puter<br />
but fails when deployed in real-world (and real slow network) conditions. One especially good<br />
method for solving this problem is with the jQuery ready() function, as you’ll see later.<br />
The basis of <strong>JavaScript</strong>’s close coupling with a webpage is through the Document Object Model<br />
(DOM). Just as your perception of the elements on the page <strong>com</strong>es through a text editor or Visual<br />
Studio, the DOM represents the programmatic or browser view of the elements on a page. Much of<br />
what you do as a <strong>JavaScript</strong> programmer is work with the DOM. Unfortunately, the DOM works in<br />
slightly different ways depending on the web browser being used to render the page. Of course, if<br />
you’ve done any HTML and CSS work, you’re already familiar with the different and nuanced ways<br />
in which browsers render pages. The same is true for <strong>JavaScript</strong>. You’ll spend a nontrivial amount of<br />
time either writing for various browsers or troubleshooting why something isn’t working in a given<br />
browser.<br />
For anything but the most basic scripts, you should use external <strong>JavaScript</strong> files. This has the advantage<br />
of providing reusability, ease of programming, and separation of HTML from programming<br />
logic. When using an external script (which is how most of this book’s examples will be constructed<br />
in later chapters), you use the src attribute of the tag to point the browser at a particular<br />
Java Script file, in much the same way you use the src attribute of an tag to specify the location<br />
of an image in an HTML page. You’ll see how to specify external scripts in more detail later in this<br />
chapter, but here’s an example:<br />
<br />
CHAPTER 1 What Is <strong>JavaScript</strong>? 9