Tài liệu JavaScript & DHTML Cookbook ppt

606 1.8K 1
Tài liệu JavaScript & DHTML Cookbook ppt

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

www.it-ebooks.info JavaScript & DHTML Cookbook ™ www.it-ebooks.info Other resources from O’Reilly Related titles Adding Ajax CSS Cookbook ™ CSS: The Definitive Guide Dynamic HTML: The Defini- tive Reference HTML & XHTML: The Definitive Guide JavaScript: The Definitive Guide Learning JavaScript oreilly.com oreilly.com is more than a complete catalog of O’Reilly books. You’ll also find links to news, events, articles, weblogs, sample chapters, and code examples. oreillynet.com is the essential portal for developers interested in open and emerging technologies, including new platforms, pro- gramming languages, and operating systems. Conferences O’Reilly brings diverse innovators together to nurture the ideas that spark revolutionary industries. We specialize in documenting the latest tools and systems, translating the in- novator’s knowledge into useful skills for those in the trenches. Visit conferences.oreilly.com for our upcoming events. Safari Bookshelf (safari.oreilly.com) is the premier online refer- ence library for programmers and IT professionals. Conduct searches across more than 1,000 books. Subscribers can zero in on answers to time-critical questions in a matter of seconds. Read the books on your Bookshelf from cover to cover or sim- ply flip to the page you need. Try it today for free. www.it-ebooks.info JavaScript & DHTML Cookbook ™ SECOND EDITION Danny Goodman Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo www.it-ebooks.info JavaScript and DHTML Cookbook ™ , Second Edition by Danny Goodman Copyright © 2007, 2003 Danny Goodman. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (safari.oreilly.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com. Editor: Tatiana Apandi Production Editor: Laurel R.T. Ruma Proofreader: Audrey Doyle Indexer: Ellen Troutman Zaig Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrators: Robert Romano and Jessamyn Read Printing History: April 2003: First Edition. August 2007: Second Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. The Cookbook series designations, JavaScript and DHTML Cookbook, the image of a howler monkey, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. This book uses RepKover ™ , a durable and flexible lay-flat binding. ISBN-10: 0-596-51408-5 ISBN-13: 978-0-596-51408-2 [M] www.it-ebooks.info v Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii 1. Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.1 Concatenating (Joining) Strings 4 1.2 Improving String Handling Performance 6 1.3 Accessing Substrings 7 1.4 Changing String Case 8 1.5 Testing Equality of Two Strings 9 1.6 Testing String Containment Without Regular Expressions 11 1.7 Testing String Containment with Regular Expressions 13 1.8 Searching and Replacing Substrings 14 1.9 Using Special and Escaped Characters 15 1.10 Reading and Writing Strings for Cookies 17 1.11 Converting Between Unicode Values and String Characters 20 1.12 Encoding and Decoding URL Strings 21 1.13 Encoding and Decoding Base64 Strings 23 2. Numbers and Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 2.1 Converting Between Numbers and Strings 31 2.2 Testing a Number’s Validity 33 2.3 Testing Numeric Equality 34 2.4 Rounding Floating-Point Numbers 35 2.5 Formatting Numbers for Text Display 36 2.6 Converting Between Decimal and Hexadecimal Numbers 39 2.7 Generating Pseudorandom Numbers 41 2.8 Calculating Trigonometric Functions 41 2.9 Creating a Date Object 42 www.it-ebooks.info vi | Table of Contents 2.10 Calculating a Previous or Future Date 43 2.11 Calculating the Number of Days Between Two Dates 45 2.12 Validating a Date 47 3. Arrays and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 3.1 Creating a Simple Array 54 3.2 Creating a Multidimensional Array 56 3.3 Converting Between Arrays and Strings 57 3.4 Doing Something with the Items in an Array 59 3.5 Sorting a Simple Array 61 3.6 Combining Arrays 63 3.7 Dividing Arrays 64 3.8 Creating a Custom Object 65 3.9 Simulating a Hash Table for Fast Array Lookup 69 3.10 Doing Something with a Property of an Object 71 3.11 Sorting an Array of Objects 72 3.12 Customizing an Object’s Prototype 74 3.13 Converting Arrays and Custom Objects to Strings 79 3.14 Using Objects to Reduce Naming Conflicts 82 4. Variables, Functions, and Flow Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 4.1 Creating a JavaScript Variable 85 4.2 Creating a Named Function 89 4.3 Nesting Named Functions 92 4.4 Creating an Anonymous Function 93 4.5 Delaying a Function Call 94 4.6 Branching Execution Based on Conditions 97 4.7 Handling Script Errors Gracefully 101 4.8 Improving Script Performance 103 5. Browser Feature Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 5.1 Detecting the Browser Brand 113 5.2 Detecting an Early Browser Version 113 5.3 Detecting the Internet Explorer Version 115 5.4 Detecting the Mozilla Version 116 5.5 Detecting the Safari Version 118 5.6 Detecting the Opera Version 119 5.7 Detecting the Client Operating System 120 5.8 Detecting Object Support 121 www.it-ebooks.info Table of Contents | vii 5.9 Detecting Object Property and Method Support 124 5.10 Detecting W3C DOM Standard Support 126 5.11 Detecting the Browser Written Language 127 5.12 Detecting Cookie Availability 128 5.13 Defining Browser- or Feature-Specific Links 129 5.14 Testing on Multiple Browser Versions 130 6. Managing Browser Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 6.1 Living with Browser Window Control Limitations 135 6.2 Setting the Main Window’s Size 136 6.3 Positioning the Main Window 137 6.4 Maximizing the Main Window 138 6.5 Creating a New Window 139 6.6 Bringing a Window to the Front 143 6.7 Communicating with a New Window 144 6.8 Communicating Back to the Main Window 147 6.9 Using Internet Explorer Modal/Modeless Windows 148 6.10 Simulating a Cross-Browser Modal Dialog Window 151 6.11 Simulating a Window with Layers 158 7. Managing Multiple Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 7.1 Creating a Blank Frame in a New Frameset 178 7.2 Changing the Content of One Frame from Another 179 7.3 Changing the Content of Multiple Frames at Once 181 7.4 Replacing a Frameset with a Single Page 182 7.5 Avoiding Being “Framed” by Another Site 183 7.6 Ensuring a Page Loads in Its Frameset 184 7.7 Reading a Frame’s Dimensions 187 7.8 Resizing Frames 188 7.9 Setting Frameset Specifications Dynamically 192 8. Dynamic Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 8.1 Auto-Focusing the First Text Field 197 8.2 Performing Common Text Field Validations 198 8.3 Preventing Form Submission upon Validation Failure 204 8.4 Auto-Focusing an Invalid Text Field Entry 207 8.5 Using a Custom Validation Object 208 8.6 Changing a Form’s Action 213 8.7 Blocking Submissions from the Enter Key 214 www.it-ebooks.info viii | Table of Contents 8.8 Advancing Text Field Focus with the Enter Key 215 8.9 Submitting a Form by an Enter Key Press in Any Text Box 216 8.10 Disabling Form Controls 217 8.11 Hiding and Showing Form Controls 219 8.12 Allowing Only Numbers (or Letters) in a Text Box 221 8.13 Auto-Tabbing for Fixed-Length Text Boxes 223 8.14 Changing select Element Content 224 8.15 Copying Form Data Between Pages 227 9. Managing Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 9.1 Equalizing the IE and W3C Event Models 236 9.2 Initiating a Process After the Page Loads 240 9.3 Appending Multiple Load Event Handlers 242 9.4 Determining the Coordinates of a Click Event 244 9.5 Preventing an Event from Performing Its Default Behavior 248 9.6 Blocking Duplicate Clicks 251 9.7 Determining Which Element Received an Event 252 9.8 Determining Which Mouse Button Was Pressed 254 9.9 Reading Which Character Key Was Typed 256 9.10 Reading Which Noncharacter Key Was Pressed 257 9.11 Determining Which Modifier Keys Were Pressed During an Event 260 9.12 Determining the Element the Cursor Rolled From/To 262 9.13 Synchronizing Sounds to Events 266 10. Page Navigation Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 10.1 Loading a New Page or Anchor 271 10.2 Keeping a Page Out of the Browser History 273 10.3 Using a select Element for Navigation 274 10.4 Passing Data Between Pages via Cookies 276 10.5 Passing Data Between Pages via Frames 278 10.6 Passing Data Between Pages via URLs 280 10.7 Creating a Contextual (Right-Click) Menu 283 10.8 Creating Drop-Down Navigation Menus 291 10.9 Providing Navigation Trail Menus 305 10.10 Creating Expandable Menus 308 10.11 Creating Collapsible XML Menus 320 www.it-ebooks.info Table of Contents | ix 11. Managing Style Sheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 11.1 Assigning Style Sheet Rules to an Element Globally 333 11.2 Assigning Style Sheet Rules to a Subgroup of Elements 334 11.3 Assigning Style Sheet Rules to an Individual Element 336 11.4 Importing External Style Sheets 337 11.5 Importing Browser- or Operating System-Specific Style Sheets 338 11.6 Changing Imported Style Sheets After Loading 340 11.7 Enabling/Disabling Style Sheets 341 11.8 Toggling Between Style Sheets for an Element 342 11.9 Overriding a Style Sheet Rule 343 11.10 Turning Arbitrary Content into a Styled Element 344 11.11 Creating Center-Aligned Body Elements 345 11.12 Reading Effective Style Sheet Property Values 346 11.13 Forcing Recent Browsers into Standards-Compatibility Mode 348 12. Visual Effects for Stationary Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351 12.1 Precaching Images 354 12.2 Swapping Images (Rollovers) 356 12.3 Reducing Rollover Image Downloads with JavaScript 358 12.4 Reducing Rollover Image Downloads with CSS 362 12.5 Dynamically Changing Image Sizes 366 12.6 Changing Text Style Properties 367 12.7 Offering Body Text Size Choices to Users 370 12.8 Creating Custom Link Styles 374 12.9 Changing Page Background Colors and Images 375 12.10 Hiding and Showing Elements 378 12.11 Adjusting Element Transparency 379 12.12 Creating Transition Visual Effects 381 12.13 Drawing Charts in the Canvas Element 385 13. Positioning HTML Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392 13.1 Making an Element Positionable in the Document Space 397 13.2 Connecting a Positioned Element to a Body Element 398 13.3 Controlling Positioning via a DHTML JavaScript Library 400 13.4 Deciding Between div and span Containers 407 13.5 Adjusting Positioned Element Stacking Order (Z-order) 409 13.6 Centering an Element on Top of Another Element 410 13.7 Centering an Element in a Window or Frame 412 www.it-ebooks.info [...]... now, more powerful applications—word processors, spreadsheets, email clients—are being built with JavaScript and DHTML JavaScript in the browser was originally designed for small scripts to work on small client-side tasks It is still used that way quite a bit around the Web Not every application is a mega DHTML app Therefore, this collection of recipes still has plenty of small tasks in mind At the same... book into your product’s documentation does require permission We appreciate, but do not require, attribution An attribution usually includes the title, author, publisher, and ISBN For example: JavaScript & DHTML Cookbook, Second Edition, by Danny Goodman Copyright 2007 Danny Goodman, 978-0-59651408-2.” If you feel your use of code examples falls outside fair use or the permission given here, feel free... sense of JavaScript They are string values, which, as it turns out, lets the strings use all of the properties and methods of the global String object which inhabits every scriptable browser window Use string values for all of your JavaScript text manipulation In a few rare instances, however, a JavaScript string value isn’t quite good enough You may encounter this situation if you are using JavaScript. .. Content During Page Loading Creating New Page Content Dynamically Including External HTML Content Embedding XML Data Embedding Data As JavaScript Objects Transforming XML Data into HTML Tables Transforming JavaScript Objects into HTML Tables Converting an XML Node Tree to JavaScript Objects Creating a New HTML Element Creating Text Content for a New Element Creating Mixed Element and Text Nodes Inserting... at least rudimentary knowledge of client-side JavaScript concepts You know how to put scripts into a web page—where tags go, as well as how to link an external js file into the current page You also know what variables, strings, numbers, Booleans, arrays, and objects are—even if you don’t necessarily remember the precise way they’re used with the JavaScript language This book is not a tutorial,... While surfing the Web, you may have encountered some scripted DHTML effect that you’d like to implement or adapt for your own pages, but either you can’t decipher the code you see or you want to “roll your own” version to avoid copyright problems with the code’s original owner If the effect or technique you’ve seen is fairly popular, this cookbook probably has a recipe for it You can use these recipes... object get good workouts with recipes for tasks such as embedding JavaScript and XML data within a document, transforming data into renderable HTML content, and sorting HTML tables instantly on the client Additional dynamic content recipes come in Chapter 15, Dynamic Content Applications, where more complex recipes show you how to use DHTML for a slide show, a user-editable document, and a pop-up calendar... even if Microsoft didn’t seem to be in a hurry to follow suit in all cases Two factors have contributed to the rebirth of interest in JavaScript and Dynamic HTML The first is the wide proliferation of broadband connections Implementing large client-side applications in JavaScript can take a bunch of code, all of which must be downloaded to the browser At dial-up speeds, piling a 50–75 kilobyte script... Introduction A string is one of the fundamental building blocks of data that JavaScript works with Any script that touches URLs or user entries in form text boxes works with strings Most document object model properties are string values Data that you read or write to a browser cookie is a string Strings are everywhere! The core JavaScript language has a repertoire of the common string manipulation properties... characters with the quote symbols See Recipe 1.9 for examples of escaped character usage in JavaScript strings If you need to include only one instance of a single or double quote within a string (e.g., "Welcome to Joe's Diner."), you can do so without special characters This is because upon encountering the start of a string, JavaScript treats ensuing characters— up to the next occurrence of the same quote . www.it-ebooks.info JavaScript & DHTML Cookbook ™ www.it-ebooks.info Other resources from O’Reilly Related titles Adding Ajax CSS Cookbook ™ CSS: The. to the page you need. Try it today for free. www.it-ebooks.info JavaScript & DHTML Cookbook ™ SECOND EDITION Danny Goodman Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo www.it-ebooks.info JavaScript

Ngày đăng: 17/02/2014, 15:20

Từ khóa liên quan

Mục lục

  • JavaScript & DHTML Cookbook

    • Table of Contents

    • Preface

      • About You

      • About the Recipes

      • What’s in This Book

      • Browser Platforms

      • Conventions Used in This Book

      • Using Code Examples

      • Request for Comments

      • Safari® Enabled

      • Acknowledgments

      • Strings

        • 1.0 Introduction

          • Creating a String

          • Regular Expressions

          • 1.1 Concatenating (Joining) Strings

            • Problem

            • Solution

            • Discussion

            • See Also

            • 1.2 Improving String Handling Performance

              • Problem

              • Solution

              • Discussion

              • 1.3 Accessing Substrings

                • Problem

Tài liệu cùng người dùng

Tài liệu liên quan