02_574973 ftoc.qxd 9/9/04 10:42 AM Page vi

TABLE OF CONTENTS

HOW TO USE THIS BOOK ...... xii

1 INTRODUCING JAVASCRIPT ...... 2 Introduction to JavaScript ...... 2 Enter a JavaScript Statement into the ...... 4 Embed JavaScript in an HTML Document ...... 6 Link an External JavaScript File ...... 8 Present Content to Non-JavaScript Enabled Browsers ...... 10 Add a Comment to JavaScript Code ...... 12 Protect the Source Code ...... 14 2 USING VARIABLES AND ARRAYS ...... 16 Understand Variable Types ...... 16 Declare Variables ...... 18 Assign Values to Variables ...... 19 Display Integers ...... 20 Display Floating-Point Numbers ...... 21 Display Booleans ...... 22 Display Strings ...... 23 Determine Variable Type ...... 24 Convert Strings to Numbers ...... 26 Convert Numbers to Strings ...... 27 Declare an Array ...... 28 Declare a Multidimensional Array ...... 30 Determine the Number of Elements of an Array ...... 32 Convert an Array into a String ...... 34 Sort an Array ...... 36 Remove Elements from an Array ...... 38 Add Elements to anCOPYRIGHTED Array ...... 39 MATERIAL 3 CREATING EXPRESSIONS ...... 40 Using the Arithmetic Operator ...... 40 Increment and Decrement Values ...... 42 Create Comparison Expressions ...... 44 Create Logical Expressions ...... 46

vi 02_574973 ftoc.qxd 9/9/04 10:42 AM Page vii

Identify Numbers ...... 48 Evaluate an Expression ...... 50 Inform the User with an Alert ...... 51 Enable User Input with a Prompt Dialog Box ...... 52 Enable User Decisions with a Confirm Dialog Box ...... 54 4 HANDLING EVENTS ...... 56 JavaScript Events ...... 56 Detect a Mouse Click ...... 58 Create Rollover Buttons ...... 60 Detect a Key Press ...... 62 Detect a Modifier Key Combination ...... 64 Set and Remove Focus ...... 66 Onchange Event ...... 68 Handle the Page Load and Onunload Operations ...... 70 Execute a JavaScript Statement from an HTML Link ...... 72 Attach an Event to an Object ...... 73 Determine the Element That Received the Event ...... 74 Determine Which Mouse Was Pressed ...... 76 Cancel Browser Events ...... 77 5 CONTROLLING PROGRAM FLOW ...... 78 Limit Executions with If-Else Statements ...... 78 Using Conditional Operators ...... 80 Optimize Performance with a Switch Statement ...... 81 Create a For Loop ...... 82 Continue and Break Loops ...... 83 Handle While Loops ...... 84 Create a Timed Interval ...... 86 Set a Regularly Timed Interval ...... 88 Declare and Call a Function ...... 90 Handle Global and Local Variables ...... 91 Pass Parameters to a Function ...... 92 Return a Value from a Function ...... 93 Increase Script Performance ...... 94

vii 02_574973 ftoc.qxd 9/9/04 10:42 AM Page viii

TABLE OF CONTENTS

6 UTILIZING STRINGS ...... 96 Determine the Length of a String ...... 96 Select Portions of a String ...... 98 Change the Case of a String ...... 100 Extract Characters from a String ...... 102 Escape a Text String ...... 104 Encode a URI ...... 105 Regular Expression ...... 106 Match the Character in a String ...... 108 Replace Characters in a String ...... 110 7 WORKING WITH HTML FORMS ...... 112 Develop HTML Forms ...... 112 Reference Form Elements ...... 113 Validate a Value ...... 114 Validate a Password Field ...... 116 Work with a Hidden Element ...... 118 Validate Text Area Input ...... 120 Work with a Button Element ...... 121 Apply Reset and Submit Buttons ...... 122 Alter Check Box Properties ...... 124 Determine Selected ...... 126 Work with a Selection List ...... 128 Basic Form Validation ...... 130 Create a Select Element Navigation ...... 132 Block an Invoked Form Submission ...... 134 Advance the Text Field Focus with the Enter Key ...... 136 8 WORKING WITH DATES AND TIMES ...... 138 Use the Date Object to Display Dates ...... 138 Get the Date and Time Components ...... 140 Set the Date and Time Parts ...... 142 Convert Dates to Strings ...... 144 Check If a Date Is in a Range ...... 146 Convert Between Time Zones ...... 147 Create a Countdown Timer ...... 148 Create a Running Clock with Images ...... 150

viii 02_574973 ftoc.qxd 9/9/04 10:42 AM Page ix

9 USING THE MATH OBJECT ...... 152 Employ Mathematical Constants ...... 152 Apply Trigonometric Functions ...... 154 Square Root and Power ...... 156 Find Minimum and Maximum Values ...... 157 Generate Random Numbers ...... 158 Round Numbers Using Methods ...... 160 Format Large Numbers with Commas ...... 162 10 EXPANDING FUNCTIONALITY WITH THE OBJECT ...... 164 Write Content to the Window ...... 164 Create a Pop-Up Window ...... 166 Detect If a Pop-Up Window Is Open ...... 168 Reference Information from Multiple Windows ...... 169 Close Pop-Up and Parent Windows ...... 170 Set Window Size and Placement ...... 172 Maximize the Browser Window ...... 174 Center the Window on the Screen ...... 176 Utilize an IE ...... 178 Create Content in a Pop-Up Window ...... 180 11 CONTROLLING THE FRAME OBJECT ...... 182 Create Frames ...... 182 Reference Individual Frames ...... 184 Determine the Frame Dimensions ...... 186 Print Individual Frames ...... 187 Break In and Out of Frames ...... 188 Resize the Frames ...... 190 Write Content to a Frame ...... 192 Understand Frame Security ...... 194 12 CREATING AND MANIPULATING OBJECTS ...... 196 Reference Objects with the Document Object Model (DOM) ...... 196 Create Custom Objects ...... 198 Develop a Custom Object Method ...... 200

ix 02_574973 ftoc.qxd 9/9/04 10:42 AM Page x

TABLE OF CONTENTS

Create a Pseudo Hash Table ...... 202 Manage Images with the Image Object ...... 204 Locate All Links ...... 206 Dissect the Location Object ...... 207 Create a Bread Crumb Navigation Menu ...... 208 Explore the Page History of the Browser ...... 210 Disable the Back Button ...... 211 13 DETERMINING ENVIRONMENT PROPERTIES . . . . .212 Detect the Browser Brand Name ...... 212 Determine the Operating System ...... 214 Distinguish the Default Native Browser Language ...... 216 Identify the JavaScript Version Support ...... 217 Verify If a Method or Object Is Supported ...... 218 Set a Cookie Value ...... 220 Retrieve a Cookie Value ...... 222 Delete a Cookie Value ...... 224 Create a Query String ...... 226 Convert a Query String into Variables ...... 228 14 CREATING CASCADING STYLE SHEETS ...... 230 Link Style Sheets to a Web Page ...... 230 Create Rules in a Style Sheet ...... 232 Add Compliance Declaration ...... 234 Attach Multiple CSS Rules ...... 235 Override Rule Properties with Style Attribute ...... 236 Modify Border Properties ...... 237 Customize a Link Style ...... 238 Center Content on the Web Page ...... 240 Add a Background Image to the Document ...... 241 Add and Alter Elements to a Web Page ...... 242 Create a Scrollable DIV ...... 244 Add an iFrame to the Web Page ...... 245 Add Transparency to an Element ...... 246 Change Style Sheet After the Page Loads ...... 248 Turn Style Sheet Off or On ...... 250

x 02_574973 ftoc.qxd 9/9/04 10:42 AM Page xi

15 CREATING INTERACTIVE WEB PAGES WITH DHTML ...... 252 Reference an HTML Element ...... 252 Create New HTML Elements ...... 254 Use innerHTML ...... 256 Set an Element Position ...... 257 Find Nonpositioned Element Positions ...... 258 Show and Hide Elements ...... 260 Find Browser Dimensions ...... 262 Find the Mouse Position ...... 263 Animate Elements ...... 264 Create a Draggable Element ...... 266 Create a Cross-Browser Layer Modal Window ...... 270 Create a Custom Alert or Confirm Dialog Box ...... 272 16 ADDING DYNAMIC CONTENT ...... 276 Insert and Remove Items in a Selection List ...... 276 Create a Double Combo Selection List ...... 278 Add New Table Rows ...... 280 Develop an Image Gallery ...... 282 Produce an Autoscrolling Window ...... 284 Construct a Navigation Tree ...... 286 Generate a Navigation Drop-Down Menu ...... 290 Insert Page Transition Effects ...... 294 Incorporate Sound with a Button ...... 296 CHAPTER 17: DEBUGGING JAVASCRIPT ERRORS . . . . .298 Detect a JavaScript Error ...... 298 Debug through the ...... 300 Solve Problem with Page Onload Handlers ...... 302 Locate Common Errors ...... 304 Avoid Errors with Try/Catch Statements ...... 305 Test JavaScript Code for Errors ...... 306 APPENDIX ...... 308

INDEX ...... 312

xi