Using a cookbook approach, The "JavaScript Anthology will show you how to apply JavaScript to solve a multitude of common Web Development challenges. You'll get answers to 101 questions ranging from "How can I format the time into a 12 or 24-hour clock?" to "How can I make my scripts run faster?"Inc
The JavaScript Anthology: 101 Essential Tips, Tricks & Hacks
β Scribed by Cameron Adams, James Edwards,
- Year
- 2006
- Tongue
- English
- Leaves
- 612
- Edition
- 1
- Category
- Library
No coin nor oath required. For personal study only.
β¦ Synopsis
Using a cookbook approach, The JavaScript Anthology will show you how to apply JavaScript to solve over 101 common Web Development challenges. You'll discover how-to: Optimize your code so that it runs faster Create Ajax applications with the XmlHttpRequest object Validate web forms to improve usability Take control of your web pages with the DOM Ensure that your JavaScript code is accessible Create slick drop-down menu systems Included in this book is extensive coverage of DHTML and Ajax, including how-to create and customize advanced effects such as draggable elements, dynamically sorting data in a Web Browser, advanced menu systems, retrieving data from a Web Server using XMLHttpRequest and more. The JavaScript Anthology also includes extensive coverage of object oriented coding, efficient script design, accessibility, and cross-browser issues. Best of all, you'll get download access to all the code used in the book, so you can put the scripts to use instantly. From the Publisher "Take control with the ultimate JavaScript toolkit" The JavaScript Anthology: 101 Essential Tips, Tricks & Hacks provides you with tried and tested real-world solutions to over 100 real-world scripting problems. Among the 101 Tips, Tricks & Hacks you'll learn how-to: Search and replace text using regular expressions. Navigate the DOM and create, delete, and move elements on the page. Validate email addresses on your web forms. Print inline error messages when validating forms. Minimize the problems associated with popup windows. Make a slideshow of images. Ensure your code works on different browsers. Make a style sheet switcher. Build an accessible drop-down menu system. Construct drag 'n' drop interfaces using AJAX. Use JavaScript and Flash together. Make your JavaScript accessible: an in-depth look at minimizing the accessibility problems associated with using JavaScript. Use the XMLHttpRequest object to build AJAX applications. Optimize your JavaScript code so that it runs faster. And much more! Who Should Read This Book?If you're using JavaScript on your projects right now, and you want to do things faster and better, this book is for you. The JavaScript Anthology will save you the frustration of hunting down code on the Web only to find that it isn't customizable, and doesn't represent best practice or work across different browsers. The JavaScript Anthology: 101 Essential Tips, Tricks & Hacks contains thoroughly tested, cross-browser code that you can easily modify to suit your own needs. The book is written in the usual SitePoint style: it's clear and fun to read, with plenty of example code that you can apply immediately to your own web sites. Plus, it's super-easy to navigate the book to find exactly what you want thanks to its cookbook approach and professionally-produced index. It's the perfect reference book. There's no need to re-type any of the code in the book. As always, customers receive instant download access to all the files used in the book, so you can apply them immediately to your own projects.
β¦ Table of Contents
Cover......Page 1
Summary of Contents......Page 3
Table of Contents......Page 11
Who Should Read this Book?......Page 19
Whatβs in this Book?......Page 20
The SitePoint Forums......Page 23
Acknowledgements......Page 24
JavaScript Defined......Page 25
JavaScriptβs Limitations......Page 26
Providing for Users who Donβt Have JavaScript (Progressive Enhancement)......Page 29
Separating Content from Behavior (Unobtrusive Scripting)......Page 32
Using Braces and Semicolons (Consistent Coding Practice)......Page 35
Adding a Script to a Page......Page 36
Getting Multiple Scripts to Work on the Same `Γ΅ βtΓ£......Page 38
Hiding JavaScript Source Code......Page 42
Debugging a Script......Page 43
Understanding a Browserβs Built-in Error Reporting......Page 44
Using alert......Page 47
Using try-catch......Page 48
Writing to the Page or Window......Page 49
Strict Warnings......Page 50
Summary......Page 53
Doing Math with JavaScript......Page 55
Rounding a Number to x Decimal Places......Page 57
Creating and Constraining Random Numbers......Page 59
Converting a Number to a String......Page 60
Formatting Currency Values......Page 62
Converting a String to a Number......Page 63
Converting Numbers to Ordinals (-st, -nd, -rd, -th)......Page 66
Summary......Page 67
Including a Special Character in a String......Page 69
Encoding a URL......Page 71
Comparing Two Strings......Page 72
Finding a Substring within a String......Page 75
Splitting a String into Substrings......Page 76
Creating a Regular Expression......Page 77
Testing whether a String Matches a Regular Expression......Page 81
Testing whether a String Contains Only Numeric DaDΕ‘Γ
Γ......Page 82
Testing whether a String is a Valid Phone Number......Page 83
Testing whether a String is a Valid Email Address......Page 84
Searching and Replacing Text using a Regular Expression......Page 85
Summary......Page 87
Working with Arrays......Page 89
Creating an Array of Arrays......Page 90
Indexing an Array with Strings Instead of Numbers......Page 93
Turning an Array into a String......Page 95
Adding or Removing Members from an Array......Page 96
Sorting an Array into Alphabetical or Numeric Order......Page 99
Sorting a Multi-dimensional Array......Page 100
Sorting an Array Randomly......Page 101
Summary......Page 102
Navigating the Document Object Model......Page 103
Accessing Elements......Page 106
Creating Elements and Text Nodes......Page 111
Changing the Type of an Element......Page 115
Removing an Element or Text Node......Page 117
Reading and Writing the Attributes of an Element......Page 119
Getting all Elements with a Particular Attribute Value......Page 122
Adding and Removing Multiple Classes to/from an Element......Page 124
Processing and Validating Forms......Page 127
Reading and Setting the State of a Checkbox......Page 130
Reading and Setting the State of a Radio Button......Page 133
Reading and Setting the Value of a Select Box......Page 135
Validating a Mandatory Text Field......Page 137
Validating a Numeric Field......Page 138
Checking for Unselected Radio Buttons......Page 139
Stopping a Form Being Submitted Unless all its Fields are Valid......Page 140
Validating a Form with an Unknown Number of Fields......Page 141
Printing Inline Error Messages when Validating a Form......Page 143
Making Form Fields Appear or Disappear, Based on the Value of other Fields......Page 145
Summary......Page 149
Working with Windows and Frames......Page 151
Whatβs Wrong with Popups?......Page 152
How Do I Minimize the Problems?......Page 153
Opening Off-site Links in a New Window......Page 157
Communicating Between Frames......Page 159
Getting the Scrolling Position......Page 161
Making the Page Scroll to a Particular Position......Page 164
Getting the Viewport Size (the Available Space inside the Window)......Page 165
Summary......Page 166
Writing Cookies......Page 167
Reading a Cookie......Page 169
Setting a Cookie to Expire at a Specific Date and Time......Page 170
Making a Cookie Accessible Only from a Specific Domain or Path......Page 171
Circumventing Browser Restrictions on the Number of Cookies you can Use......Page 172
Summary......Page 174
Getting the Date and Time......Page 175
Formatting a Date into a Sentence......Page 178
Formatting the Time into a 12- or 24-hour Clock......Page 181
Comparing Two Dates......Page 183
Formatting the Difference Between Dates......Page 188
Summary......Page 190
Preloading Images......Page 191
Swapping One Image for Another......Page 193
Displaying an Image at Random......Page 195
Making a Slideshow of Several Images......Page 197
Making an Image Fade in or out......Page 200
Making an Image-based Clock that Updates in Real Time......Page 205
Making a Progress Indicator......Page 210
Summary......Page 213
Detecting Browser Differences......Page 215
Identifying Support for a Particular Feature......Page 216
Identifying a Particular Browser......Page 218
Detecting Quirks Mode and Standards Mode......Page 222
Summary......Page 224
Changing the Style of a Single Element......Page 225
Changing the Style of a Group of Elements......Page 227
Retrieving the Computed Style of an Element......Page 228
Making a Style Sheet Switcher......Page 231
Maintaining Alternate Style Sheet States......Page 236
Making a Style Sheet Switcher that Handles Multiple Media Types......Page 239
Reading and Modifying an Existing Style Sheet......Page 241
Adding New Style Sheet Rules......Page 244
Deleting a Rule from a Style Sheet......Page 247
Creating a New Style Sheet......Page 248
Summary......Page 251
Basic Dynamic HTML......Page 253
The Short Way: Using Event Handlers......Page 254
The W3C Way (Event Listeners)......Page 257
Referencing the Target Element......Page 262
What is Event Bubbling, and How do I Control it?......Page 267
Finding the Size of an Element......Page 269
Finding the Position of an Element......Page 270
Detecting the Position of the Mouse Cursor......Page 272
Displaying a Tooltip when you Mouse Over an Element......Page 274
Sorting Tables by Column......Page 281
Summary......Page 290
Using setTimeout and setInterval......Page 291
Making an Object Move Along a Set Path......Page 294
Making Animation Less Jerky......Page 302
Changing Between Frames......Page 303
The Speed of the Computer......Page 304
Implementing Drag-and-drop Behavior......Page 305
Reordering a List Using Drag-and-drop Functionality......Page 314
Making a Scrolling News Ticker......Page 322
Creating Clip-based Transition Effects......Page 329
Making a Slider Control......Page 335
Summary......Page 342
DHTML Menus and Navigation......Page 345
Making a Drop-down or Fly-out Menu......Page 347
Adding Arrows to Indicate the Presence of a Submenu......Page 358
Adding Timers so the Menus Donβt Open and Close so Abruptly......Page 362
Making Sure the Menus Stay Inside the Window......Page 369
Making the Menus Display Over select Elements......Page 378
Making a Folder Tree or Expanding Menu......Page 385
Indicating Expanded Branches in a Menu......Page 395
Allowing Only One Menu Branch to Be Open at Any Time......Page 401
Opening the Current Sub-branch Automatically......Page 402
Summary......Page 407
JavaScript and Accessibility......Page 409
What is Accessibility?......Page 410
Who are the Affected Users?......Page 411
Making Scripts Accessible to the Keyboard......Page 413
Using Device-independent Event Handlers......Page 417
Making Scripts Accessible to the Keyboard as well as the Mouse......Page 419
Rollovers and Revealing Content......Page 420
Form Validation......Page 422
Drag-and-drop Functionality......Page 424
AJAX and other Remote Scripting Techniques......Page 425
Making title Attribute Tooltips Display on Focus......Page 426
Making a DHTML Menu Accessible to the Keyboard......Page 435
Making a DHTML Menu Usable via the Keyboard......Page 445
Making a DHTML Slider Control Accessible to the Keyboard......Page 452
Making Scripts Accessible to Screen Readers......Page 460
JavaScript Behaviors......Page 462
Rollovers and Revealing Content......Page 463
Form Validation......Page 464
Non-user-initiated Scripts......Page 465
AJAX and other Remote Scripting Techniques......Page 470
Tricks and Hacks......Page 473
Reaching toward a Best Practice Approach......Page 478
Using JavaScript with Flash......Page 481
Communicating Between JavaScript and Flash......Page 485
Flash/JavaScript Integration Kit......Page 488
Summary......Page 489
Building Web Applications with JavaScript......Page 491
Retrieving Data Using XMLHttpRequest......Page 492
Requesting Data from a Server......Page 494
Parsing the Data......Page 497
Caching......Page 499
Retrieving Data without Using XMLHttpRequest......Page 500
Creating Custom Dialogs (Such as Popup Forms)......Page 505
Creating Editable Elements......Page 513
Controlling Text Selections......Page 520
Creating an Auto-complete Text Field......Page 526
Summary......Page 538
Object Orientation in JavaScript......Page 539
Encapsulation......Page 540
Class Inheritance......Page 541
Object Based Code vs Object Oriented Code......Page 542
Writing an Object Oriented Script......Page 543
Creating Methods for an Object......Page 545
Prototype-based Method Creation......Page 546
Modelling Inheritance......Page 550
Understanding Scope......Page 552
Implementing Namespaces......Page 555
Summary......Page 557
Keeping up the Pace......Page 559
Saving References to Objects you Use Frequently......Page 560
Using Ternary Operators and Switch Statements......Page 563
Optimizing Loops......Page 566
Avoiding eval......Page 567
Avoiding Strict Warnings......Page 568
Optimizing for a Particular Browser......Page 569
Dividing Tasks into Functions (or Using OO)......Page 572
Using Arrays and Iteration to Avoid Code Repetition......Page 574
Compacting Conditions and Return Statements......Page 0
Removing Comments and Unnecessary Whitespace......Page 576
Compacting the Names of Variables and Properties......Page 579
Avoiding Memory Leaks......Page 580
Avoiding Circular References......Page 581
Cleaning Up After the Fact......Page 582
Making Scripts Run Before the Load Event......Page 584
Summary......Page 587
Index......Page 589
π SIMILAR VOLUMES
This book came highly recommended at a recent programming conference. So, I suppose that my expectations were set too high. If you have been working with CSS for awhile, then I do not think that you will find many new and exciting tips in this book. However, if you are new to CSS, then it can
<DIV><p> The PHP Anthology: 101 Essential Tips, Tricks & Hacks , 2nd Edition is a collection of powerful PHP 5 solutions to the most common programming problems. </p><p> Featuring best-practice code and a commonsense approach to development, this book includes coverage of: </p>Manage errors gracefu
The PHP Anthology: 101 Essential Tips, Tricks & Hacks, 2nd Edition is a collection of powerful PHP 5 solutions to the most common programming problems. Featuring best-practice code and a commonsense approach to development, this book includes coverage of:Manage errors gracefully. Build functional f