Programmer to Programmer™ Get more out of WROX.com Interact Chapters on Demand Take an active role online by participating in Purchase individual book chapters in pdf our P2P forums format Wrox Online Library Join the Community Hundreds of our books are available online Sign up for our free monthly newsletter at through Books24x7.com newsletter.wrox.com Wrox Blox Browse Download short informational pieces and Ready for more Wrox? We have books and code to keep you up to date and out of e-books available on .NET, SQL Server, Java, trouble! XML, Visual Basic, C#/ C++, and much more!Contact Us.We always like to get feedback from our readers. Have a book idea?Need community support? Let us know by e-mailing email@example.com
In memory of my mum, June Wilton, who in 2006 lost her brave battle against cancer. She was always very proud of me and my books and showed my books to anyone and everyone she happened to meet however brieﬂy and whether they wanted to see them or not! She’s very much missed. — Paul Wilton To my family: Starla, Hayden, and Evan (whom we haven’t yet met in person). To my parents: Jerry and Judy. Thank you all for your love and support. — Jeremy McPeak
About the Authors Paul Wilton started as a Visual Basic applications programmer at the Ministry of Defense in the UK and then found himself pulled into the Net. Having joined an Internet development company, he spent three years helping create Internet solutions. He’s now running his own successful and rapidly growing com- pany developing online holiday property reservation systems. Jeremy McPeak is a self-taught programmer who began his career by tinkering with web sites in 1998. He is the co-author of Professional Ajax, 2nd Edition (Wiley 2007) and several online articles covering top- ics such as XSLT, ASP.NET WebForms, and C#. He is currently employed in an energy-based company building in-house conventional and web applications. Jeremy can be reached through his web site www.wdonline.com.
ContentsChapter 3: Decisions, Loops, and Functions 51 Decision Making — The if and switch Statements 51 Comparison Operators 52 The if Statement 54 Logical Operators 58 Multiple Conditions Inside an if Statement 60 else and else if 64 Comparing Strings 66 The switch Statement 67 Looping — The for and while Statements 71 The for Loop 71 The for...in Loop 74 The while Loop 75 The do...while loop 77 The break and continue Statements 78 Functions 79 Creating Your Own Functions 79 Variable Scope and Lifetime 83 Summary 84 Exercise Questions 86Chapter 4: Common Mistakes, Debugging, and Error Handling 87 D’oh! I Can’t Believe I Just Did That: Some Common Mistakes 87 Undeﬁned Variables 88 Case Sensitivity 89 Incorrect Number of Closing Braces 90 Incorrect Number of Closing Parentheses 90 Using Equals (=) Rather than Is Equal To (==) 91 Using a Method as a Property and Vice Versa 91 Missing Plus Signs During Concatenation 92 Error Handling 93 Preventing Errors 93 The try … catch Statements 94 Debugging 103 Debugging in Firefox with Firebug 104 Debugging in Internet Explorer 116 Debugging in Safari 123 Using Dragonﬂy: Opera’s Development Tools 127 Summary 131 Exercise Questions 131xvi
Contents Common Properties and Methods 224 Button Elements 226 Text Elements 229 The textarea Element 236 Check Boxes and Radio Buttons 239 Selection Boxes 247 Summary 260 Exercise Questions 262Chapter 8: Windows and Frames 263 Frames and the window Object 264 Coding Between Frames 268 Code Access Between Frames 273 Opening New Windows 284 Opening a New Browser Window 284 Scripting Between Windows 292 Moving and Resizing Windows 297 Security 298 Summary 298 Exercise Questions 299Chapter 9: String Manipulation 301 Additional String Methods 302 The split() Method 302 The replace() Method 305 The search() Method 305 The match() Method 306 Regular Expressions 306 Simple Regular Expressions 307 Regular Expressions: Special Characters 315 Covering All Eventualities 323 Grouping Regular Expressions 323 Reusing Groups of Characters 325 The String Object — split(), replace(), search(), and match() Methods 327 The split() Method 327 The replace() Method 329 The search() Method 332 The match() Method 333xviii
Contents Using the RegExp Object’s Constructor 335 Telephone Number Validation 339 Validating a Postal Code 341 Validating an E-mail Address 342 Validating a Domain Name 342 Summary 345 Exercise Questions 345Chapter 10: Date, Time, and Timers 347 World Time 348 Setting and Getting a Date Object’s UTC Date and Time 351 Timers in a Web Page 358 One-Shot Timer 359 Setting a Timer that Fires at Regular Intervals 363 Summary 364 Exercise Questions 365Chapter 11: Storing Information: Cookies 367 Baking Your First Cookie 367 A Fresh-Baked Cookie 368 The Cookie String 374 Creating a Cookie 377 Getting a Cookie’s Value 381 Cookie Limitations 386 A User May Disable Cookies 386 Number and Information Limitation 387 Cookie Security and IE6+ 388 Summary 389 Exercise Questions 389Chapter 12: Dynamic HTML and the W3C Document Object Model 391 The Web Standards 392 HTML 394 XML 394 XHTML 395 ECMAScript 396 xix
IntroductionTry It Out The Try It Out is an exercise you should work through, following the text in the book. 1. It usually consists of a set of steps. 2. Each step has a number. 3. Follow the steps with your copy of the database. As you work through each Try It Out, the code you’ve typed will be explained in detail. Boxes like this one hold important, not-to-be forgotten information that is directly relevant to the surrounding text. Tips, hints, tricks, and asides to the current discussion are offset and placed in italics like this. As for styles in the text: ❑ We highlight in italic type new terms and important words when we introduce them. ❑ We show keyboard strokes like this: Ctrl+A. ❑ We show ﬁle names, URLs, and code within the text like so: persistence.properties. ❑ We present code in two different ways: Important code in code examples is highlighted with a gray background. The gray highlighting is not used for code that’s less important in the present context, or that has been shown before.Source Code As you work through the examples in this book, you may choose either to type in all the code manually or to use the source-code ﬁles that accompany the book. All of the source code used in this book is avail- able for download at www.wrox.com. Once at the site, simply locate the book’s title (either by using the Search box or by using one of the title lists) and click the Download Code link on the book’s detail page to obtain all the source code for the book. Because many books have similar titles, you may ﬁnd it easiest to search by ISBN; this book’s ISBN is 978-0-470-52593-7. Once you download the code, just decompress it with your favorite compression tool. Alternately, you can go to the main Wrox code download page at www.wrox.com/dynamic/books/download.aspx to see the code available for this book and all other Wrox books. xxvii