1. Trang chủ
  2. » Công Nghệ Thông Tin

JavaScript FOR ™ DUMmIES phần 1 ppsx

39 202 0

Đ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

Thông tin cơ bản

Tiêu đề JavaScript For Dummies
Tác giả Emily Vander Veer
Thể loại sách
Định dạng
Số trang 39
Dung lượng 0,98 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Some of the people who helped bring this book to market include the following: Acquisitions, Editorial, and Media Development Project Editor: Pat O’Brien Acquisitions Editor: Steven Haye

Trang 2

by Emily Vander Veer

FOR

4 TH EDITION

Trang 4

JavaScript ™

FOR

4 TH EDITION

Trang 6

by Emily Vander Veer

FOR

4 TH EDITION

Trang 7

JavaScriptFor Dummies, ® 4th Edition

Published by

Wiley Publishing, Inc.

111 River Street Hoboken, NJ 07030-5774 Copyright © 2005 by Wiley Publishing, Inc., Indianapolis, Indiana Published by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or

by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permis- sion of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600 Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, e-mail: brandreview@wiley.com.

Trademarks: Wiley, the Wiley Publishing logo, For Dummies, the Dummies Man logo, A Reference for the

Rest of Us!, The Dummies Way, Dummies Daily, The Fun and Easy Way, Dummies.com, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc and/or its affiliates in the United States and other countries, and may not be used without written permission JavaScript is a trademark of Sun Microsystems, Inc All other trademarks are the property of their respective owners Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book.

LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO RESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE NO WARRANTY MAY BE CRE- ATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS THE ADVICE AND STRATEGIES CON- TAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM THE FACT THAT AN ORGANIZATION

REP-OR WEBSITE IS REFERRED TO IN THIS WREP-ORK AS A CITATION AND/REP-OR A POTENTIAL SOURCE OF THER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT

FUR-IS READ FULFILLMENT OF EACH COUPON OFFER FUR-IS THE SOLE RESPONSIBILITY OF THE OFFEROR.

For general information on our other products and services, please contact our Customer Care Department within the U.S at 800-762-2974, outside the U.S at 317-572-3993, or fax 317-572-4002.

For technical support, please visit www.wiley.com/techsupport Wiley also publishes its books in a variety of electronic formats Some content that appears in print may not be available in electronic books.

Library of Congress Control Number: 2004107963 ISBN: 0-7645-7659-3

Manufactured in the United States of America

10 9 8 7 6 5 4 3 2 1 4B/QS/RR/QU/IN

Trang 8

About the Author

Freelance author and Web guru Emily A Vander Veer has penned severalbooks and countless articles on Internet-related technologies and trends.You can e-mail her at eav@outtech.com

Trang 11

Publisher’s Acknowledgments

We’re proud of this book; please send us your comments through our online registration form located at www.dummies.com/register.

Some of the people who helped bring this book to market include the following:

Acquisitions, Editorial, and Media Development

Project Editor: Pat O’Brien Acquisitions Editor: Steven Hayes Copy Editor: Virginia Sanders Technical Editor: Craig Lukasik Editorial Manager: Kevin Kirschner Media Development Manager:

Joyce Haughey, Jacque Roth, Heather Ryan

Special Art:

Proofreaders: Carl Pierce, Joe Niesen,

TECHBOOKS Production Services

Indexer: TECHBOOKS Production Services

Publishing and Editorial for Technology Dummies Richard Swadley, Vice President and Executive Group Publisher Andy Cummings, Vice President and Publisher

Mary Bednarek, Executive Acquisitions Director Mary C Corder, Editorial Director

Publishing for Consumer Dummies Diane Graves Steele, Vice President and Publisher Joyce Pepple, Acquisitions Director

Composition Services Gerry Fahey, Vice President of Production Services Debbie Stailey, Director of Composition Services

Trang 12

Contents at a Glance

Introduction 1

Part I: Building Killer Web Pages for Fun and Profit 7

Chapter 1: Hitting the Highlights: JavaScript Basics 9

Chapter 2: Writing Your Very First Script 23

Chapter 3: JavaScript Language Basics 35

Chapter 4: JavaScript-Accessible Data: Getting Acquainted with the Document Object Model 73

Part II: Creating Dynamic Web Pages 103

Chapter 5: Detecting Your Users’ Browser Environments 105

Chapter 6: That’s How the Cookie Crumbles 125

Chapter 7: Working with Browser Windows and Frames 143

Part III: Making Your Site Easy For Visitors to Navigate and Use 155

Chapter 8: Creating Interactive Images 157

Chapter 9: Creating Menus 181

Chapter 10: Creating Expandable Site Maps 191

Chapter 11: Creating Pop-Up Help (Tooltips) 201

Part IV: Interacting with Users 213

Chapter 12: Handling Forms 215

Chapter 13: Handling User-Initiated Events 239

Chapter 14: Handling Runtime Errors 249

Part V: The Part of Tens 253

Chapter 15: Top Ten (Or So) Online JavaScript Resources 255

Chapter 16: Ten (Or So) Most Common JavaScript Mistakes (And How to Avoid Them) 261

Chapter 17: Ten (Or So) Tips for Debugging Your Scripts 273

Trang 13

Part VI: Appendixes 293

Appendix A: JavaScript Reserved Words 295

Appendix B: JavaScript Color Values 297

Appendix C: Document Object Model Reference 303

Appendix D: Special Characters 329

Appendix E: About the CD 335

Index 341

Trang 14

Table of Contents

Introduction 1

System Requirements 1

About This Book 2

Conventions Used in This Book 2

What You’re Not to Read 3

Foolish Assumptions 4

How This Book Is Organized 4

Part I: Building Killer Web Pages for Fun and Profit 4

Part II: Creating Dynamic Web Pages 4

Part III: Making Your Site Easy for Visitors to Navigate and Use 5

Part IV: Interacting with Users 5

Part V: The Part of Tens 5

Part VI: Appendixes 5

Icons Used in This Book 5

Where to Go from Here 6

Part I: Building Killer Web Pages for Fun and Profit 7

Chapter 1: Hitting the Highlights: JavaScript Basics 9

What Is JavaScript? (Hint: It’s Not the Same Thing as Java!) 10

It’s easy! (Sort of) 11

It’s speedy! 13

Everybody’s doing it! (Okay, almost everybody!) 13

JavaScript and HTML 14

JavaScript and Your Web Browser 16

What Can I Do with JavaScript That I Can’t Do with Web Languages? 17

Make your Web site easy for folks to navigate 18

Customize the way your Web site looks on-the-fly 18

Create cool, dynamic animated effects 19

What Do I Need to Get Started? 19

Hardware 19

Software 20

Documentation 21

Trang 15

Chapter 2: Writing Your Very First Script 23

From Idea to Working JavaScript Application 24

Ideas?! I got a million of ’em! 24

Part I: Creating an HTML file 25

Part II: Creating your script 29

Part III: Putting it all together by attaching a script to an HTML file 30

Testing Your Script 32

Chapter 3: JavaScript Language Basics 35

JavaScript Syntax 35

Don’t keep your comments to yourself 36

Fully functioning 42

Operators are standing by 50

Working with variables 56

Putting It All Together: Building JavaScript Expressions and Statements 58

The browser-detection script 59

The date-formatting script 64

The data-gathering script 68

Chapter 4: JavaScript-Accessible Data: Getting Acquainted with the Document Object Model 73

Object Models Always Pose Nude 74

Object-ivity 75

For sale by owner: Object properties 77

There’s a method to this madness! 79

How do you handle a hungry event? With event handlers! 81

Company functions 82

Anatomy of an Object: Properties, Methods, Event Handlers, and Functions in Action 84

Dynamic objects: The least you need to know about CSS and DHTML 84

Example DHTML script: Adding text dynamically 86

Example DHTML script: Positioning text dynamically 90

Example DHTML script: Changing page appearance on-the-fly 93

Browser Object Models 96

Netscape Navigator 96

JavaScript data types 98

Microsoft Internet Explorer 100

JavaScript For Dummies, 4th Edition

xii

Trang 16

Part II: Creating Dynamic Web Pages 103

Chapter 5: Detecting Your Users’ Browser Environments 105

Whacking Your Way through the Browser Maze 105

Detecting Features 106

Browser make and version 106

Embedded objects 112

The referrer page 121

User preferences 122

Chapter 6: That’s How the Cookie Crumbles 125

Cookie Basics 125

Why use cookies? 126

Cookie security issues 126

Looking at cookies from a user’s perspective 127

Saving and Retrieving User Information 131

Setting a cookie 132

Accessing a cookie 133

Displaying content based on cookie contents: The repeat-visitor script 134

Chapter 7: Working with Browser Windows and Frames 143

Working with Browser Windows 144

Opening and closing new browser windows 144

Controlling the appearance of browser windows 147

Working with Frames 148

Creating HTML frames 149

Sharing data between frames 152

Part III: Making Your Site Easy For Visitors to Navigate and Use 155

Chapter 8: Creating Interactive Images 157

Creating Simple Animations 157

Now you see it, now you don’t: Turning images on and off 161

Slideshow Bob: Displaying a series of images 165

Creating Rollovers, Hotspots, and Navigation Bars 168

Creating a simple rollover 169

Creating navigation bars by putting rollovers together 171

Carving up a single image into multiple hotspots 177

xiii

Table of Contents

Trang 17

Chapter 9: Creating Menus 181

Getting Acquainted with Menus 182

Pull-down menus 182

Sliding menus 186

Taking Advantage of Third-Party DHTML Menu Components 190

Chapter 10: Creating Expandable Site Maps 191

Site Map Basics 191

The pull-down menu revisited 193

Adding frames to the pull-down menu 196

Putting it all together: Adding targeted hyperlinks 197

Taking Advantage of Third-Party Site-Mapping Tools 199

Chapter 11: Creating Pop-Up Help (Tooltips) 201

Creating Plain HTML Tooltips 202

Building DHTML Tooltips 204

Creating an HTML map and designating active areas 204

Defining a style for the tooltip 205

Creating custom JavaScript functions to display and hide tooltips 206

Calling custom functions in response to the onMouseOver and onMouseOut events 207

Putting it all together: Using DHTML code to create simple tooltips 209

Taking Advantage of Third-Party Tooltips Scripts 211

Part IV: Interacting with Users 213

Chapter 12: Handling Forms 215

Capturing User Input by Using HTML Form Fields 215

Creating an input-validation script 216

Calling a validation script 221

Putting It All Together: The Order Form Validation Script 222

Testing for existence 224

Testing for a numeric value 225

Testing for patterns 227

Form-level validation 228

Chapter 13: Handling User-Initiated Events 239

The Skinny on Events and Event Handlers 239

Handling Events 240

Window events 243

Mouse events 244

Form events 245

Keyboard events 247

JavaScript For Dummies, 4th Edition

xiv

Trang 18

Chapter 14: Handling Runtime Errors 249

Exceptional Basics 249

Handling Exceptions 250

Part V: The Part of Tens 253

Chapter 15: Top Ten (Or So) Online JavaScript Resources 255

Ten Web Sites to Check Out 255

Netscape 256

Microsoft 256

Builder.com 256

Webmonkey 256

Project Cool’s JavaScript QuickStarts 256

EarthWeb.com 257

About.com 257

IRT.org 257

WebReference.com 258

ScriptSearch.com 258

Not-to-Be-Missed Newsgroups 258

Chapter 16: Ten (Or So) Most Common JavaScript Mistakes (And How to Avoid Them) 261

Typing-in-a-Hurry Errors 262

Breaking Up a Happy Pair 263

Lonely angle brackets 263

Lonely tags 263

Lonely parentheses 264

Lonely quotes 265

Putting Scripting Statements in the Wrong Places 265

Nesting Quotes Incorrectly 266

Treating Numbers as Strings 267

Treating Strings as Numbers 268

Missing the Point: Logic Errors 269

Neglecting Browser Incompatibility 270

Chapter 17: Ten (Or So) Tips for Debugging Your Scripts 273

JavaScript Reads Your Code, Not Your Mind! 274

Isolating the Bug 275

Consulting the Documentation 276

Displaying Variable Values 276

Breaking Large Blocks of Statements into Smaller Functions 279

Honing the Process of Elimination 280

Debugging browser problems 281

Tracking HTML bugs 281

Checking the JavaScript code 282

xv

Table of Contents

Trang 19

Taking Advantage of Others’ Experience 282

Exercising the Time-Honored Trial-and-Error Approach 283

Just Try and Catch Me Exception Handling! 283

Taking Advantage of Debugging Tools 287

Netscape’s JavaScript console 288

Microsoft Internet Explorer’s built-in error display 290

Part VI: Appendixes 293

Appendix A: JavaScript Reserved Words 295

Appendix B: JavaScript Color Values 297

Appendix C: Document Object Model Reference 303

The Document Object Model 303

Anchor 304

Applet 304

Area 305

arguments 305

Array 305

Boolean 306

Button 306

Checkbox 306

clientInformation 307

crypto 307

Date 308

document 308

elements[] 309

event 309

FileUpload 310

Form 310

Frame 311

Function 311

Hidden 311

History 312

Image 312

java 312

JavaArray 313

JavaClass 313

JavaObject 313

JavaPackage 313

Link 314

location 314

Math 314

MimeType 315

JavaScript For Dummies, 4th Edition

xvi

Trang 20

netscape 316

Number 316

Object 316

Option 317

Packages 317

Password 318

Plugin 318

Radio 318

RegExp 319

Reset 320

screen 320

Select 320

String 321

Style 321

Submit 322

sun 323

Text 323

Textarea 323

window 324

Global Properties 325

Built-In JavaScript Functions 325

escape( ) 325

eval( ) 325

isFinite( ) 326

isNaN( ) 326

Number( ) 326

parseFloat( ) 326

parseInt( ) 327

String( ) 327

taint( ) 327

unescape( ) 328

untaint( ) 328

Appendix D: Special Characters 329

Appendix E: About the CD 335

Getting the Most from This CD 335

System Requirements 336

Using the CD 336

JavaScript For Dummies Chapter Files 337

What You’ll Find 337

If You Have Problems (Of the CD Kind) 338

Index 341

xvii

Table of Contents

Ngày đăng: 14/08/2014, 11:20