Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
CHAPTER 4 JavaScript Primer jQuery is a JavaScript library that you add to your HTML documents. On its own, the jQuery library doesn't do anything. You take advantage of the jQuery functionality from your own scripts that you add alongside the jQuery library. In this chapter, I provide a primer for the JavaScript language, focusing on the features that are most pertinent when working with jQuery. JavaScript has had a difficult life and was rushed through standardization before it had a chance to mature properly. This has resulted in a language that can be very pleasant to work with but that has some odd behavioral and syntactical quirks. Tip To get the best from this book, you will need some experience of programming and an understanding of concepts such as variables, functions, and objects. If you are new to programming, a good starting point is a series of articles posted on the popular Lifehacker.com. No programming knowledge is assumed, and all of the examples are conveniently in JavaScript. The guide is available here: http://lifehacker.com/5744113/learn- to-code-the-full-beginners-guide . Getting Ready to Use JavaScript You can define scripts in an HTML document in a couple of different ways. You can define an inline script, where the content of the script is part of the HTML document. You can also define an external script, where the JavaScript is contained in a separate file and referenced via a URL (which is how you access the jQuery library, as you'll see in Chapter 5). Both of these approaches rely on the script element. In this chapter, I will be using inline scripts. You can see a simple example in Listing 4-1. Listing 4-1. A Simple Inline Script <!DOCTYPE HTML> <html> <head> <title>Example</title> <script type="text/javascript"> console.log("Hello"); 67