SKIP THE SHIPPING
Use code NOSHIP during checkout to save 40% on eligible eBooks, now through January 5. Shop now.
Register your product to gain access to bonus material or receive a coupon.
By award-winning Web developer Dan Barrett, whose clients have included Hewlett-Packard, Electronic Arts, and Pacific Bell.
Don't waste your time or money on a gigantic JavaScript book! Learn the techniques you'll really useright now, hands on, by example, from one of the world's top Web developers. You'll master JavaScript by building actual practice sites that sell products, educate visitors, offer subscriptions, display adseven enforce security. Best of all, the finished sites are on the Webso you can reuse the code, and see exactly how your pages should look and work Fully updated for the latest standards and browsers, Essential JavaScript for Web Professionals, Second Edition is simply the fastest way to gain the JavaScript skills you need most.
You'll master all this and much more:
Rely on Essential Guides for ALL the Web Skills You Need!All these books share the same great format and the same dynamic Web site ... so once you've used one, they're all a piece of cake!
Download all the project files (ZIP file format, 2.7 MB) from Essential JavaScript for Web Professionals, 2/e.
JavaScript for Web Professionals: Using Image Rollovers
Click here for a sample chapter for this book: 0131001477.pdf
Introduction.
Acknowledgments.
About the Author.
1. Dynamism and Detection.
Project I: Browser Detection and Cloaking-Dynamically Generating Platform-Specific Content. Creating External CSS Files. Debugging JavaScript Code. Inserting a Script into Your HTML. JavaScript Objects and Hierarchies. Conditional Statements. Reviewing the Script.
Project II: Dynamic Content Creation-Printing Out the Copyright Information and Last-Modified Date. External JavaScript Files. Introduction of the Last-Modified Property. Dynamically Printing Nonstring Data. Reviewing the Script. Recap. Advanced Projects.
2. Image Rollovers.
Project I: Image Rollover Script. Creating and Inserting the Image Objects. Image Rollover Functions. Creating and Inserting the Event Handlers. Reviewing the Script.
Project II: Multiple Image Rollover Script. Creating and Inserting the Image Objects. Inserting the Rollover Functions. Creating and Inserting the Event Handlers. Reviewing the Script.
Project III: Random Banner Ad Rotator Script. Creating and Inserting the Image Objects. Random Image Generator Function. Dynamically Printing Out the Image. Reviewing the Script. Recap. Advanced Projects.
3. JavaScript for Navigation.
Project I: JavaScript and Pull-Down Menus-Getting to Where You Want to Go! Creating the Navigation Function. Inserting the Event Handler. Using onChange for Instant Gratification. Reviewing the Script.
Project II: Using Multiple Pull-Down Menus for Navigation. Creating the Arrays. Creating the Drill-Down Menu Function. Creating the Navigation Function. Inserting the Event Handlers. Reviewing the Script.
Project III: Using JavaScript on a Log-in Page. Inserting the Event Handler. Reviewing the Script.
Project IV: Using CSS/DHTML with JavaScript for Navigation. Introduction to CSS/DHTML. Creating the Menu Layers. Creating the Navigation Function. Creating and Inserting the Event Handlers. Reviewing the Script. Recap. Advanced Projects.
4. Error Handling in Forms.
Project I: Dynamically Selecting Multiple Form Fields. Creating the Change Checkbox State Function. Inserting the Event Handlers. Reviewing the Script.
Project II: Error Handling of Forms. Checking Forms for Empty Text Box Fields. Making Sure a Pull-Down Menu Option Was Chosen. Checking Email Addresses for . and @. Checking for Area Codes in Phone Numbers. Making Sure Zip Codes Have Only Numbers.
Project III: Letting the User Know What's Wrong. Reviewing the Script. Recap. Advanced Projects.
5. JavaScript Windows and Frames.
A Look at the WINDOW Object. Project I: Creating, Populating, and Closing Windows with JavaScript. Creating and Defining the Arrays. Creating the Function. Inserting the Event Handlers. Reviewing the Script.
Project II: Using JavaScript to Talk Between Frames. Figuring Out Your Frame Hierarchy. Creating a Function to Check Which Section You Are In. Inserting the Event Handler. Reviewing the Script.
Project III: Making Sure Your Frames Site Shows Up in Frames. Checking for the Presence of Frames. Reloading the Page within the Frameset. Reviewing the Script. Recap. Advanced Projects.
6. Advanced JavaScript.
Project I: Interactive Photo Slideshow. Creating and Inserting the Image Objects. Creating the Photo Arrays. Creating the Slideshow Functions. Inserting the Event Handlers. Reviewing the Script.
Project II: Searchable Designer Database. Functional Specification. Multidimensional Arrays. Creating and Filling the Arrays. Creating the Search Functions. Inserting the Event Handlers. Reviewing the Script. Recap. Advanced Projects.
Appendix A. Event Handlers.
Appendix B. JavaScript Objects.
Index.
Welcome! This book is something I wish I had had when I was first starting out with JavaScript. At that time, there were basically two types of instructional books on the market: 1,200-page tomes of seemingly arcane knowledge and books that were overly simplified and lacking in practical information. Unfortunately, there were no books that were informative and at the same time provided instruction that could be used quickly and effectively in real-world situations.
This book guides you through JavaScript using examples taken straight from situations that are faced every day during Web site construction. It starts with simpler examples and becomes quite sophisticated with the scripting toward the end of the book. If you're a novice, it's probably best to start at the beginning and work up to the projects in the later chapters. A lot of what is accomplished in the more advanced chapters builds off of what is learned in the first chapters of the book.
Chances are that at least some of you picked up this book when your boss called you into his or her office and showed you a Web site that made use of JavaScript. You were then told in no uncertain terms that it was your job to implement the same or similar functionality on your company's Web site. Believe me, I know how this goes, "No problem," you respond, while saying to yourself, "Gee, I'd better learn JavaScript and fast!"
This is often the way in which we expand our skills: We are given a job, and if we don't know exactly how to do it, we quickly learn how. In keeping with this real-world model, this book is split into two main sections. In each main section, we create and/or upgrade the Web sites for one of two fictitious companies.
In the first three chapters, we revamp the Web site of Shelley Biotechnologies, a fast-growing biotech startup. Each chapter has at least one project that consists of commonly used JavaScript solutions that range from easy to moderately difficult. At the end of each chapter are more advanced exercises that you can complete on your own to expand your skills. In the second half of the book, we make some much-needed additions to Stitch Magazine's Web site. Stitch is a popular fashion magazine that is trying to get the online version up to the same par as the hardcopy version of the magazine. The examples are, for the most part, more advanced than those found in the first section and show you some of the powerful things you can do using JavaScript.
The exercises in the book are designed to give you a solid foundation in JavaScript on which you can build as you continue to use it. You will find that more often than not there is more than one way to do things in JavaScript.
As an extra bonus, we have created a Web site where you can go to download all of the code and images needed to follow along with each project in the book. You can only learn so much from reading how to do something; the true way to learn is to get your hands dirty and do the work. The companion site for this book can be found at http://www.phptr.com/essential/javascript/
. I encourage you to go to the site and work along with the real code as you move through each project.
For those of you who are new to the world of Web development and are perhaps learning JavaScript in conjunction with HTML, a quick rundown of what JavaScript is may be in order. JavaScript is Netscape's built-in cross-platform scripting language. Like HTML, it works on all platforms. JavaScript, however, allows you to enhance the functionality of your Web pages by embedding applications directly into your HTML. You can use JavaScript to build applications that range from adding interactivity to your pages to interacting with databases. Although Netscape created JavaScript, it has become an integral part of creating Web pages, and all of the major browsers, including Internet Explorer, support it in one fashion or another. When Microsoft first implemented support for JavaScript (which they labeled Jscript), it was buggy and unreliable. As JavaScript became more prevalent, it was decided to take a shot at standardizing the language so that all implementations would share common functionality. The standardized version of the language was put together by ECMA, an international standards body. As of Internet Explorer's (IE) 4.0 release of Jscript and Netscape's release of version 1.3 of JavaScript, both implementations are ECMA-compliant. Of course, as newer versions of IE have come out, Microsoft has introduced new extensions, most of which are not supported in any versions of Netscape lower that 6x. This constant bickering between the companies has made the life of the Web developer more challenging, to say the least. Often, you will find the perfect solution to a problem only to later realize that the solution is supported only in one of the two browsers; then you must go searching around for a solution for the other browser. This is one of the biggest drawbacks to using JavaScript; however, with the release of Netscape 6.x, the gap between the different implementations has closed a bit. Now all we need to do is wait until all of the Internet's users have upgraded their browsers to the latest and greatest versions. On second thought, don't hold your breath on that. For the foreseeable future, these differences are something we are going to have to deal with; lucky for you, we have come up with several ways to handle these issues. We will use them throughout the book when we have to treat code differently for the different browsers.
With the whole can't-they-just-get-along speech out of the way, let's look at the methods by which JavaScript can be implemented on the Web. There are two methods that you can use to include JavaScript in your Web pages: client-side and server-side. Both methods share the same basic language set. This core language defines a base set of objects and features that work in both client-side and server-side applications. Each method also has extended objects and feature sets that only apply to it.
Server-side JavaScript applications are stored on the Web server and must be called up from each page that wishes to access them. Using this method gives you two advantages. First, you are using the server's processing power to run the script, so if it's a really beefy script, your server can probably process it faster than the user machine. Second, by using server-side scripts, you have better access to the other applications running on the server. That being said, however, client-side scripts make up at least 90 percent of the scripts you see on the Web. If you really want to get into server-side applications, there are other more robust languages on the market that would better serve your needs, so in this book, we focus on client-side JavaScript.
Client-side JavaScript applications are scripts that are embedded directly into your HTML pages and are executed by the user's browser as it loads the page. When the user's browser calls up an HTML page with JavaScript embedded in it, the browser's JavaScript runtime engine is called to interpret the script, which it reads from top to bottom, executing statements as it goes.
One of the main advantages of using client-side scripting is that the script is able to detect and make use of user-initiated events, such as changes to a form or the mouse rolling over a particular graphic. The script is then able to use that information to call other parts of the script, and all of this can be done without going back to the Web server and grabbing more information.
Now that we know a little bit about the different methods of JavaScript, let's look at how the language is set up. You can pretty much break the language into two sections, the first being objects and the functionality that deals with them, and second, the language elements that let you manipulate them. We don't want to get too deeply into these at this point, but a quick look at what each section consists of will be helpful as we start our scripting.
In the first section, we can find the following:
The second section contains the following:
This is just a brief look at how the JavaScript language is arranged. How these different parts of JavaScript all come together to create our scripts will be revealed throughout our projects. Before we move onto our first project, a quick word on some of the limitations of JavaScript is in order.
While the applications that you can create using JavaScript are limited only by your imagination, there are several things that you cannot do. Most notably is accessing or controlling the user's machine. For security reasons, writing to a user's computer is severely limited. Only through the use of a cookie can you store any data on the user's machine, and even then you are limited to a simple text file. This protects users from scripts that might do their computers harm or allow unscrupulous programmers access to personal information.
A security feature called the Same Origin Policy also restricts the access of a script from one origin access to certain properties or files at other locations. For example, if you have a script located at http://www.yoursite.com/test.html
and it tries to access certain properties of an HTML page located at http://www.theirsite.com/test.html
, the Same Origin Policy will deny your script access.
These two areas cover the main restrictions that you will find when writing JavaScript applications. I am sure you will find yourself at times trying to use an object or property to do something that can't be done, but those limitations are less restrictions than just a matter of learning the structure of the language.
As you read this book, if you think of a possible solution that may differ from the examples, give it a shot; you can often stumble onto a solution that others may not have thought of. With all that said, let's get on with the learning.