Learn JavaScript - Chuck Easttom - · PDF fileLearn JavaScript Chuck Easttom Wordware Pub...

download Learn JavaScript - Chuck Easttom - · PDF fileLearn JavaScript Chuck Easttom Wordware Pub lishing, Inc. Library of Congress Cataloging-in-Publication Data ... Or ga ni za tional Web

If you can't read please download the document

Transcript of Learn JavaScript - Chuck Easttom - · PDF fileLearn JavaScript Chuck Easttom Wordware Pub...

  • Learn JavaScript

    Chuck Easttom

    Wordware Pub lishing, Inc.

  • Library of Congress Cataloging-in-Publication Data

    Easttom, Chuck. Learn JavaScript / by Chuck Easttom. p. cm. ISBN 1-55622-856-2 (pb) 1. Computer graphics. 2. Computer games. I. Title. T385.F447 2000 794.8'166--dc21 00-047319 CIP

    2002, Wordware Publishing, Inc.

    All Rights Reserved

    2320 Los Rios BoulevardPlano, Texas 75074

    No part of this book may be reproduced in any form or byany means without permission in writing from

    Wordware Publishing, Inc.

    Printed in the United States of America

    ISBN 1-55622-856-210 9 8 7 6 5 4 3 2 10109

    Product names mentioned are used for identification purposes only and may be trademarks of their respectivecompanies.

    All inquiries for volume purchases of this book should be addressed to Wordware Publishing, Inc., at theabove address. Telephone inquiries may be made by calling:

    (972) 423-0090

  • Contents Summary

    Chap ter 1 HTML Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

    Chap ter 2 JavaScript Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

    Chap ter 3 Alerts, Prompts, and User Feed back . . . . . . . . . . . . . . . . . . 33

    Chap ter 4 Im age Ef fects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

    Chap ter 5 Back ground Ef fects . . . . . . . . . . . . . . . . . . . . . . . . . . . 59

    Chap ter 6 The Doc u ment Ob ject Model . . . . . . . . . . . . . . . . . . . . . . 75

    Chap ter 7 Working with Date and Time . . . . . . . . . . . . . . . . . . . . . . 87

    Chap ter 8 Working with Cookies . . . . . . . . . . . . . . . . . . . . . . . . . 97

    Chap ter 9 Working with the Sta tus Bar . . . . . . . . . . . . . . . . . . . . . . 111

    Chap ter 10 Cre ating Dy namic Menus . . . . . . . . . . . . . . . . . . . . . . . 121

    Chap ter 11 Working with Forms . . . . . . . . . . . . . . . . . . . . . . . . . . 135

    Chap ter 12 Strings in JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . 149

    Chap ter 13 Math in JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . 157

    Chap ter 14 Adding Plug-ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169

    Chap ter 15 Ob jects in JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . 181

    Chap ter 16 Tips and Tricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187

    Chap ter 17 JavaScript Games . . . . . . . . . . . . . . . . . . . . . . . . . . . 197

    Chap ter 18 ActiveX and JavaScript . . . . . . . . . . . . . . . . . . . . . . . . 209

    Chap ter 19 Pro gramming and JavaScript . . . . . . . . . . . . . . . . . . . . . 217

    Chap ter 20 An tique Book store Pro ject . . . . . . . . . . . . . . . . . . . . . . . 223

    Ap pen dix A On line Re sources . . . . . . . . . . . . . . . . . . . . . . . . . . . 237

    Ap pen dix B HTML Ref er ence . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241

    iii

  • Ap pen dix C JavaScript Ref er ence . . . . . . . . . . . . . . . . . . . . . . . . . 245

    Ap pen dix D Com mon Er rors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251

    iv n Contents Summary

  • Con tents

    Ac knowl edg ments . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiIn tro duc tion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii

    Chap ter 1 HTML Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1Im ages and Hyperlinks. . . . . . . . . . . . . . . . . . . . . . . . . . 4Colors and Back grounds . . . . . . . . . . . . . . . . . . . . . . . . . 6Ta bles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9Mar quee . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Sum mary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

    Chap ter 2 JavaScript Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13Es sen tial Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

    Choosing a Browser . . . . . . . . . . . . . . . . . . . . . . . . . 14Ba sic JavaScript Struc ture . . . . . . . . . . . . . . . . . . . . . . . . 14

    The Tag. . . . . . . . . . . . . . . . . . . . . . . . . . 14Internal Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . 14External Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . 15

    Placing JavaScript Code . . . . . . . . . . . . . . . . . . . . . . . 15JavaScript Con ven tions . . . . . . . . . . . . . . . . . . . . . . . 16

    Using the Semicolon. . . . . . . . . . . . . . . . . . . . . . . . 16Case Sensitivity . . . . . . . . . . . . . . . . . . . . . . . . . . 17Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17Using Quotes . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

    Your First Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18Fun da men tal JavaScript Con cepts . . . . . . . . . . . . . . . . . . . 20

    Data Types and Vari ables in JavaScript. . . . . . . . . . . . . . . . 20JavaScript Op er a tors. . . . . . . . . . . . . . . . . . . . . . . . . 21JavaScript State ments . . . . . . . . . . . . . . . . . . . . . . . . 23

    Multiple Statements . . . . . . . . . . . . . . . . . . . . . . . . 23Nested Statements . . . . . . . . . . . . . . . . . . . . . . . . 24

    JavaScript Ex pres sions . . . . . . . . . . . . . . . . . . . . . . . . 24JavaScript Func tion Dec la ra tions. . . . . . . . . . . . . . . . . . . 24

    Calling Functions . . . . . . . . . . . . . . . . . . . . . . . . . 26Di a log Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

    Alert Boxes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28Con firm Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28Prompt Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

    v

  • if State ment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30Sum mary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

    Chap ter 3 Alerts, Prompts, and User Feed back . . . . . . . . . . . . . . . . . . 33Alert Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33Prompt Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35Writ ing Back to the Web Page . . . . . . . . . . . . . . . . . . . . . 38An tique Book store Pro ject . . . . . . . . . . . . . . . . . . . . . . . 41Sum mary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

    Chap ter 4 Im age Ef fects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43The Im age Ob ject . . . . . . . . . . . . . . . . . . . . . . . . . . . 43Roll over . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44Slide Show . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46Im age Pre view . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51Ban ner Ads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53Im age Popup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54An tique Book store Pro ject . . . . . . . . . . . . . . . . . . . . . . . 56Sum mary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

    Chap ter 5 Back ground Ef fects . . . . . . . . . . . . . . . . . . . . . . . . . . . 59Doc u ment Ob ject. . . . . . . . . . . . . . . . . . . . . . . . . . . . 59Changing the Back ground Color . . . . . . . . . . . . . . . . . . . . 59Changing the Back ground Im age. . . . . . . . . . . . . . . . . . . . 62More Back ground Ef fects . . . . . . . . . . . . . . . . . . . . . . . . 68An tique Book store Pro ject . . . . . . . . . . . . . . . . . . . . . . . 70Sum mary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

    Chap ter 6 The Doc u ment Ob ject Model . . . . . . . . . . . . . . . . . . . . . . 75win dow Ob ject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

    doc u ment Ob ject . . . . . . . . . . . . . . . . . . . . . . . . . . 76his tory Ob ject . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76

    The Doc u ment Ob ject . . . . . . . . . . . . . . . . . . . . . . . . . 77The Win dow Ob ject . . . . . . . . . . . . . . . . . . . . . . . . . . 80The Nav i ga tor Ob ject. . . . . . . . . . . . . . . . . . . . . . . . . . 83Using the His tory Ob ject . . . . . . . . . . . . . . . . . . . . . . . . 84An tique Book store Pro ject . . . . . . . . . . . . . . . . . . . . . . . 85Sum mary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86

    Chap ter 7 Working with Date and Time . . . . . . . . . . . . . . . . . . . . . . 87Time of Day . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87Day of Week . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89Set Time out . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92An tique Book store Pro ject . . . . . . . . . . . . . . . . . . . . . . . 93Sum mary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95

    Chap ter 8 Working with Cookies . . . . . . . . . . . . . . . . . . . . . . . . . 97Baking Your First Cookie . . . . . . . . . . . . . . . . . . . . . . . . 97

    vi n Con tents

  • SetCookie . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99GetCookie . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100Call ing the Func tions . . . . . . . . . . . . . . . . . . . . . . . . 101

    Bake An other Cookie . . . . . . . . . . . . . . . . . . . . . . . . . 102An tique Book store Pro ject . . . . . . . . . . . . . . . . . . . . . . . 106Sum mary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109

    Chap ter 9 Wo