Paul McFedries' Web Home


The cover of Special Edition Using JavaScript Special Edition Using JavaScript

Introduction


When the Web first came to the attention of the world's non-geeks back in the mid-1990s, the vastness and variety of its treasures were a wonder to behold. However, it didn't take long before a few courageous and intrepid souls dug a little deeper into this phenomenon and discovered something truly phenomenal: they could make Web pages, too!

Why was that so amazing? Well, think back to those old days and think, in particular, of what it meant to create what we now call content. Think about television shows, radio programs, magazines, newspapers, books, and the other media of the time. The one thing they all had in common was that their creation was a decidedly uncommon thing. It required a team of professionals, a massive distribution system, and a lot of money. In short, it wasn't something that your average Okie from Muskogee would have any hope of duplicating.

The Web appeared to change all that because learning HTML was within the grasp of anybody who could feed himself, it had a built-in massive distribution system (the Internet), and it required little or no money. For the first time in history, content was democratized and was no longer defined as the sole province of governments and mega-corporations.

Then reality set in.

People soon realized that merely building a Web site wasn't enough to attract "eyeballs," as the marketers say. A site had to have interesting, useful, or fun content, or people would stay away in droves. Not only that, but this good content had to be combined with a solid site design, which meant that Web designers needed a thorough knowledge of HTML and good design skills.

But, alas, eventually even all that was not enough. To make their Web sites work with the world's different browsers, to make their sites easy to navigate, and to give their sites those extra bells and whistles that surfers had come to expect, something more than content, HTML, and design was needed.

That missing link was JavaScript.

What we've all learned the hard way over the past few years is that you simply can't put together a world-class Web site unless you have some scripting prowess in your site design toolkit. You need to know how to script your way out of the basic problems that afflict most sites, and how to use scripts to go beyond the inherent limitations of HTML. And it isn't enough just to copy the generic scripts that are available on the Web and paste them into your pages. First of all, most of those scripts are very poorly written, and second of all they invariably need some customization to work properly on your site.

My goal in this book is to show you how to create your own JavaScript programs that are suited to your site and your content. My aim is to show you that JavaScript isn't hard to learn, and that even the most inveterate non-programmer can learn how to put together scripts that will amaze their friends (and themselves).

What You Need to Know

This book is not a primer on creating Web pages or on using the World Wide Web. This is a JavaScript book, pure and simple. This means I assume you already know the following:

  • How to operate a basic text editor, and how to get around the operating system and file system on your computer.

  • How to use your Web browser.

  • The basic HTML tags and attributes, particularly those that comprise the HTML 4 standard.

  • The basic style sheet rules and attributes, particularly those that comprise the Cascading Style Sheets 2 standard.

"I've Never Programmed Before!"

If you've never done a stitch of computer programming before, even if you're not quite sure what programming really is, don't worry about it for a second because I had you in mind when I wrote this book. For too many years programming has been the property of "hackers" and other technowizards. That made some sense because the programming languages they were using—with bizarre names such as C++ and Fortran—were exceedingly difficult to learn, and even harder to master.

JavaScript, however, is different. It's nowhere near as hard to learn as those for-nerds-only languages. I honestly believe that anyone can become a savvy and successful JavaScript programmer, and this book is, I hope, the proof of that assertion. Just follow along, examine my scripts carefully (particularly those in the first few chapters), and practice what you learn, and you will master JavaScript.

"I Have Programmed Before!"

What if you've done some programming in the past? For example, you might have dipped a toe or two in the JavaScript waters already, or you might have dabbled with Basic, Visual Basic, or some other language. Will this book be too basic for you? No, not at all. My other main goal in this book is to provide you with a ton of examples of truly useful scripts that you can customize and incorporate into your own site. The book's first few chapters start slowly to avoid scaring off those new to this programming business. But once we get past the basics, I introduce you to lots of great techniques and tricks that will take your programming skills to a higher level.

How the Book Is Structured

If you're looking for lots of JavaScript history, theory and long-winded explanations of concepts, I'm sorry but you won't find it here. My philosophy throughout this book is "It's the code, stupid!" I explain what needs to be explained and then I move on without further ado (or without any ado at all) to examples and scripts that do more to illuminate a concept that any verbose explanations I could muster (and believe me, I can muster verbosity with the best).

How you approach this book depends on your current level of JavaScript expertise (or lack thereof):

  • If you're just starting out, begin at the beginning with Chapter 1 and work at your own pace sequentially through to Chapter 12. This will give you all the knowledge you need to pick and choose what you want to learn throughout the rest of the book.

  • If you know a bit of JavaScript already, you can probably get away with taking a fast look at Chapter 2, and then working quickly through the material in Part 2. You'll then be ready to branch out and explore the rest of the book as you see fit.

  • If you're a relatively experienced JavaScript programmer, you might want to first skim through Part 2. I've got a few tricks in there that you might find interesting. After that, feel free to consider the rest of the book a kind of scripting smorgasbord that you can sample as your programming taste buds dictate.

Just so you know what to expect and to help you plan your forays into the book, the next few sections summarize the content of the book's eight parts.

Part 1—Getting Started with JavaScript

This short section includes just two chapters. Chapter 1, "An Overview of JavaScript," gives you the big JavaScript picture and explains why JavaScript is such a fundamental tool for Web designers. If you're new to programming, you'll appreciate my explanation of just what a programming language is. Chapter 2, "Creating Your First Scripts," gets down to the coding brass tacks by showing you how to add scripts to your pages and by taking you step-by-step through the process of creating a few simple scripts.

Part 2—Programming Fundamentals

Part 2, with ten chapters in all, is the longest section of the book. That's appropriate because the aim of these chapters is to provide you with a solid JavaScript foundation. And if there's a single secret to becoming a successful programmer it's this: learn the basics well. The more comfortable you are with things like variables, functions, and expressions (the topics of Chapters 3 through 5), the easier time you'll have grasping higher-level topics.

Other chapters in Part 2 cover things such as controlling your JavaScript code, objects (one of the most important JavaScript concepts), events, and arrays. I also show you some important techniques for getting your scripts to interact with your site visitors and how to troubleshoot (debug in programming lingo) script problems.

Part 3—Using JavaScript's Basic Objects

The four chapters in Part 3 tackle four indispensable JavaScript objects. The Date object (Chapter 13, "Dealing with Dates and Times: The Date Object") enables you to work with dates and times. For example, you can create simple JavaScript clocks, you can figure out the number of days from now until some date in the future, and lots more. The Math object (Chapter 14, "Working with Numbers: The Math Object") enables you to perform lots of interesting calculations, including doing useful financial calculations such as computing loan payments and figuring out the future value of an investment. The String object (Chapter 15, "Manipulating Text: The String Object") is one of the most commonly used JavaScript objects because your scripts work with text all the time. Finally, the Navigator object (Chapter 16, "Controlling the Browser: The Navigator Object") enables you to figure out some interesting information about the browser that a site visitor is using (such as the name of the browser, its version number, and what operating system it's running on).

Part 4—Working with Browser Windows

Your scripting moves into a higher gear here in Part 4. That's because these five chapters show you how to use JavaScript to manipulate the user's actual browser window. You'll learn how to display messages in the status bar, how to get the dimensions of the window, how to open new windows, and how to close them when they've served their purpose. I also show you how to send the browser to a specified page and how to fiddle with frames via JavaScript. I close with a full-chapter project that shows you various methods for using JavaScript to set up a password-protected Web page.

Part 5—Working with the Document Object

The Document object refers to the Web page that's displayed in the browser, and the five chapters in Part 5 show you a fistful of ways to work with these documents. You learn how to change document colors, work with links, and even how to write tags and text to a document, on-the-fly! If you can't wait to learn how to do those fun "mouseover" effects that are featured on many sites, then Chapter 24, "Working with Images and Mouseovers," is the place to go. I also show you how to work with cookies, and I close with another project: a navigation "tree" that makes it easy for visitors to get from here to there on your site.

Part 6—Working with Forms

One of the most common uses for JavaScript code is to work with forms, and that's just what you'll learn in the four chapters that comprise Part 6. I show you how to control form submission from your code and how to work with all the field types (text boxes, checkboxes, radio buttons, select lists, and so on). There's also an entire chapter on form validation that shows you how to do things such as ensure a field is filled in, check for valid phone numbers and email addresses, and lots more. I also take you through a complete shopping cart script.

Part 7—Working with Dynamic HTML

Dynamic HTML refers to the ability to manipulate a page's tag and text after it has already loaded. As you'll see in the five chapters that make up Part 7, this enables you to create many interesting effects, including text that scrolls across the page, image slide shows, and lots more.

Part 8—Appendixes

The book winds up with three appendixes. Appendix A, "JavaScript Tools for Debugging Scripts," shows you how to use Microsoft's Script Debugger and Netscape's JavaScript Debugger, both of which can help you fix script problems. Appendix B, "JavaScript Reserved Words and Keywords," provides you with a complete list of all the JavaScript words that you need to avoid when making up your own variable and function names. Appendix C, "Working with Regular Expressions," introduces you to the wacky but useful world of JavaScript regular expressions.

Where's the Code?

I believe wholeheartedly that the best way to learn JavaScript (or anything, for that matter) is to be given lots of good quality examples. To that end, this book contains hundreds of example scripts for you to try out and study. However, life is way too short to have to type in the examples by hand. The good news is that you don't have to because I've put every last example online at my Web site. Here's the address where you'll find everything:

http://www.mcfedries.com/UsingJavaScript/

Return to Special Edition Using JavaScript home page
Copyright © 1995-2008 Paul McFedries and Logophilia Limited