12.07.2015 Views

Pro JavaScript for Web Apps pdf - EBook Free Download

Pro JavaScript for Web Apps pdf - EBook Free Download

Pro JavaScript for Web Apps pdf - EBook Free Download

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

C H A P T E R 2Getting StartedIn this chapter, I am going to enhance the example web app I introduced in Chapter 1. These are theentry-level techniques, and most of the rest of the book is dedicated to showing you different ways toimprove upon the result. That’s not to say that the examples in this chapter are not useful; they areabsolutely fine <strong>for</strong> simple web apps. But they are not sufficient <strong>for</strong> large and complex web apps, which iswhy the chapters that follow explain how you can take key concepts from the world of server-sidedevelopment and apply them to your web apps.This chapter also lets me set the foundation <strong>for</strong> some web app development principles that I will beusing throughout this book. First, I will be relying on <strong>JavaScript</strong> libraries whenever possible so as toavoid creating code that someone else has produced and maintained. The library I will be making mostuse of is jQuery in order to make working with the DOM API simpler and easier (I explain some jQuerybasics in the examples in this chapters). Second, I will be focusing on a single HTML document.Upgrading the Submit ButtonTo get started, I am going to use <strong>JavaScript</strong> to replace the submit button from the baseline example inChapter 1. The browser creates this button from an input element whose type is submit, and I am goingto switch it out <strong>for</strong> something that is visually consistent with the rest of the document. More specifically,I am going to use jQuery to replace the input element.Preparing to Use jQueryThe DOM API is comprehensive but awkward to use—so awkward that there are a number of <strong>JavaScript</strong>convenience libraries that wrap around the DOM API and make it easier to use. In my experience, thebest of these libraries is jQuery, which is easy to use and actively developed and supported. jQuery is alsothe foundation <strong>for</strong> many other <strong>JavaScript</strong> libraries, some of which I’ll be using later. jQuery is just awrapper around the DOM API, and this allows the use of the underlying DOM objects and methods if itis required.You can download the jQuery library from jQuery.com. jQuery, like most <strong>JavaScript</strong> libraries, isavailable in two versions. The uncompressed version contains the full source code and is useful <strong>for</strong>development and debugging. The compressed version (also known as the minimized or minifiedversion) is much smaller but isn’t human-readable. The smaller size makes the minimized version ideal<strong>for</strong> saving bandwidth when a web app is deployed into production. Bandwidth can be expensive <strong>for</strong>popular web apps, and any savings is worth making.<strong>Download</strong> the version you want and put it in your content directory, alongside example.html. I’ll beusing the uncompressed version in this book, so I have downloaded a file called jquery-1.7.1.js.15www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!