Teach Yourself VBScript in 21 Days
VBScript in 21 Days
by Keith Brophy, Timothy Koets
WEEK 1 At a Glance
- The Internet and the World Wide Web
- Browsing the World Wide Web
- The Elements of the World Wide Web
- Extending the World Wide Web with VBScript
- Microsoft ActiveX and ActiveX Controls
- Extending the World Wide Web with CGI
Day 2 The Essence of VBScript
- What Is VBScript?
- What Can VBScript Do?
- Some Examples of VBScript in Action
- Learning VBScript
- Security and VBScript
- VBScript Versus Visual Basic and Visual Basic for Applications
- How VBScript Enhances Browsers and HTML
- Where VBScript Is Supported
- Embedding VBScript in an HTML Document
- The Structure of an HTML Document
- What Is a Variable?
- How Do I Create a Variable?
- What Can Variables Contain?
- Determining the Type of Data in a Variable
- Changing the Type of Data in a Variable
- Working with Constants
- The Scope and Lifetime of a Variable
- Working with Arrays
- Creating Arrays
- Working with Operators
- Arithmetic Operators
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/ and \)
- Exponents (^)
- Modulo Arithmetic (Mod)
- Negation (-)
- Comparison Operators
- Equality (=)
- Inequality (<>)
- Less Than and Greater Than (< and >)
- Less Than or Equal To and Greater Than or Equal To (<= and >=)
- Object Equivalence (Is)
- Logical Operators
- Negation (Not)
- Conjunction (And)
- Disjunction (Or)
- Exclusion (Xor)
- Logical Equivalence (Eqv) and Implication (Imp)
- Working with Strings
- Operator Precedence
- Why Control the Flow of Code?
- Using Control Structures to Make Decisions
- Using Control Structures to Make Code Repeat
- Putting It All Together
- Passing Arguments into Procedures
- Why Are Procedures Useful?
- Event Procedures
- Method Procedures
- Procedures, Subroutines, Functions, Events, and Methods!
- Where to Put Procedures
WEEK 1 In Review
WEEK 2 At a Glance
- An Introduction to HTML Forms
- The Button Control
- The Text Control
- The Textarea Control
- Putting It All Together
- The Radio Button Control
- The Check Box Control
- The Password Control
- Intrinsic HTML Form Controls at a Glance
- ActiveX Technology
- The Label Control
Day 11 More ActiveX Controls
- The New Item Control
- Timer Control
- Setting Properties for the Timer Control
- Providing Dynamic Feedback in Your Web Page with the Timer Control
- Other ActiveX Controls
- Working with Objects
- The ActiveX Chart Control
- Integrating Java Applets
- The ActiveVRML Viewer Object
- Component Downloading Issues
- Advantages of Standards and Conventions
- Standards to Use
- Within Your Code
- Script Structure
- Giving the User the Message Through Message Boxes
- Input Boxes
WEEK 2 In Review
WEEK 3 At a Glance
- Asc and Chr
- Len and Mid
- What Does VBScript Let You Do with Math?
- Abs and Sgn
- The Logarithmic Functions-Log and Exp
- The Square Root Function-Sqr
- The Trig Functions-Sin, Cos, Tan, and Atn
- Extending the Power of VBScript Through the Log and Trig Functions
- Rounding and the Integer Functions-Fix and Int
- Randomize and Rnd
- Working with Non-Decimal Systems-Hex and Oct
- The Mod Operator
- Variable Representation of Numeric Data
- VBScript Error Handling Support (or Lack Thereof!)
- Effective Error Hunting
- Tracing Your Code
- HTML Trace Capabilities
- VBScript Trace Capabilities
- Ensuring That the Bug Is Dead
- Building Your Own Trace Routines
- Error Handling to Include in Your Final Script
- More Reasons Why VBScript Can Be Tough to Debug
- The Moral of the Story
- 2-D Forms, the Layout Control, and the ActiveX Control Pad
- Loading Graphics in Advance with the ActiveX preload Control
- Anchor Clicks on Images and Text
- Browser Objects
- The Advantage of Validating Server-Bound Data
- Reducing Traffic by Scripting Dynamic Pages
- How Do Visual Basic and VBScript Compare?
- Finding Differences When You Port Code
- Syntax Elements in Visual Basic 4.0 but Not in VBScript
- Other Visual Basic and VBScript Differences
- Stability Issues
- Security Issues
WEEK 3 In Review
Appendix A VBScript Syntax Quick Reference
Appendix B Information Resources
- Update Information for This Book
- The Internet Services Provider
- General Information Resources
- Carl & Gary's Visual Basic Home Page
- CGI Specification
- Macmillan Computer Publishing
- Microsoft ActiveX Control Kit
- Microsoft Internet Download Toolbox
- Microsoft Internet Explorer
- Microsoft VBScript Information
- Microsoft VBScript Language Reference
- ncSA Mosaic
- Unofficial VBScript Information Page
- Visual Basic CGI Programming
- VBScript Observations and Samples
- World Wide Web Consortium (W3C) Drafts
- W3C Objects: "Inserting Objects into HTML" Working Draft
Appendix C Answers to Quiz Questions
- Day 1
- Day 2
- Day 3
- Day 4
- Day 5
- Day 6
- Day 7
- Day 8
- Day 9
- Day 10
- Day 11
- Day 12
- Day 13
- Day 14
- Day 15
- Day 16
- Day 17
- Day 18
- Day 19
- Day 20
- Day 21
Copyright © 1996 by Sams.net Publishing
All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Neither is any liability assumed for damages resulting from the use of the information contained herein. For information, address Sams.net Publishing, 201 W. 103rd St., Indianapolis, IN 46290.
International Standard Book Number: 1-57521-120-3
HTML conversion by :
M/s. LeafWriters (India) Pvt. Ltd.
Website : http://leaf.stpn.soft.net
e-mail : email@example.com
Interpretation of the printing code: The rightmost double-digit number is the year of the book's printing; the rightmost single-digit, the number of the book's printing. For example, a printing code of 96-1 shows that the first printing of the book occurred in 1996.
All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams.net Publishing cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark. Microsoft and Visual Basic are registered trademarks of Microsoft Corporation.
|President, Sams Publishing||Richard K. Swadley|
|Acquisitions Manager||Greg S. Wiegand|
|Managing Editor||Cindy Morrow|
|Marketing Manager||John Pierce|
|Assistant Marketing Manager||Kristina Perry|
|Acquisitions Editor||Christopher Denny||Development Editor||Anthony Amico|
|Software Development Specialist||Steve Straiger||Production Editors||Kitty Wilson Kris Simmons|
|Copy Editors||Kate Shoup, Colleen A. Williams||Technical Reviewer||Greg Guntle|
|Editorial Coordinator||Bill Whitmer||Technical Edit Coordinator||Lynette Quinn|
|Editorial Assistants||Carol Ackerman, Andi Richter, Rhonda Tinch-Mize||Cover Designer||Tim Amrhein|
|Book Designer||Gary Adair||Copy Writer||Peter Fuller|
|Production Team Supervisor||Brad Chinn|
|Production||Mary Ann Abramson,Georgiana Briggs, Cheryl Dietsch, Michael Dietsch, Chris Livengood, Paula Lowell, Mike Henry, Dana Rhodes, Andy Stone|
A popular saying in the Internet development arena nowadays states that if a technology is more than three months old, it's obsolete! This sentiment is perhaps exaggerated for effect, but it does underscore a very significant change the industry is undergoing. New software technologies and breakthroughs are coming at a breakneck, leapfrog pace. Staying current is more of a challenge than ever before. Picking the technologies that will be here to stay, as opposed to pretenders to the throne, has never been tougher.
If you have a copy of this book, though, we think you've picked a winner. VBScript opens up a world of possibilities for the environments you apply it to-Web pages in particular. You can create smart, active, and interactive Web pages with VBScript with unparalleled ease. It is difficult to appreciate the full power and impact of this new scripting language without a good resource to back it up. It's been our aim to get such a resource into your hands as soon as possible. The "under-construction dust" is just settling as this book goes to print, but already it is clear that a capable, stable, mature product has emerged.
Microsoft, like many companies in the opinionated Internet fray, has its detractors. As a large, high-profile company and somewhat of a late arrival onto the Internet scene, its technologies are the focus of great anticipation and in some corners, even skepticism. The skepticism has been unfounded. Microsoft has delivered a scripting language that is easy to use, flexible, powerful, and secure. The most prominent home of the original release of this language is the browser, which is the main focus of this book. However, you can certainly expect it to become integral to many more products as the future unfolds. VBScript is a technology that is here to stay. As the waves of technology change in the exciting months and years ahead, it is a safe bet that VBScript will be riding the tide. To some extent, the course of many other of the vessels traveling the sea might be changed because of VBScript.
Keith and Tim
This book is based on a stable beta version of Internet Explorer 3.0 and Visual Basic, Scripting Edition (also called VBScript). We've expended a lot of effort to ensure that it is as accurate as possible. However, if any discrepancies between the material covered here and the official product release of Internet Explorer 3.0 and VBScript come to light, they will be fully documented and available online. Refer to Appendix B, "Information Resources," for information on how to obtain the latest updates, if any, from Macmillan Publishing's site
Disclaimer from the Publisher
The information contained in this book is subject to the following disclaimers:
- The information in this book is based on a beta that is subject to change.
- Microsoft does not endorse the content of this book, nor will it provide support for the beta product.
- You can visit Microsoft's Web site at http://www.microsoft.com/vbscript for up-to-date information about Visual Basic, Scripting Edition, and http://www.microsoft.com/intdev for related ActiveX information and software.
We would like to thank Sams.net for the opportunity to tackle a topic that we view as a very important, exciting step in software evolution. Along with this, we extend a special thanks to Chris Denny for his help and support all along the way; Tony Amico, our key development editor; Kitty Wilson, our ever-patient production editor; Greg Guntle, technical editor; and the rest of the great Sams team behind us. Appreciation is similarly extended to the great gang at CNS, our Internet service provider and the best "voice of experience" we've found on rapidly evolving Internet connectivity issues.
We'd also like to thank our colleagues at X-Rite, Incorporated, where we develop software by day. Although this book is a project independent of the company, we certainly benefit in knowledge and spirit from our daily association with such a fine group of co-workers. Thanks especially to Steve Peterson for encouraging a view toward the leading edge of technology.
Thanks also to our friends and families. Without them, the book couldn't have come about. After spending much time on the last book saying, "It's gonna be a long time before we do this again," something unexpected happened. A fantastic topic coaxed us out of retirement and made liars of us within just a few months, and there we were, doing it again. This book came during busy times in our lives. During the time the book was written, we dealt with sick cockatiels (Tim), rescuing kids locked in the bathroom and taking them to the circus (Keith), wedding preparations (Tim), the Boston Marathon (Keith), busy work schedules (Tim and Keith), and then the wedding itself (Tim and Michelle)! We strove very hard for quality in this book and felt we never sacrificed in this regard. We did sacrifice time with family and friends, who nevertheless were right there behind us every step of the way. So much credit goes to them all.
Much thanks and love from Keith to Sue and from Tim to Michelle. This book is dedicated to them.
Keith adds: Thanks to my wife, Sue, for assisting with so many aspects of the book preparations in addition to being there every step of the way. Thanks also to Ginger, Mom, and Dad, not only for the support in general, but also for making the kids the world's happiest grandkids. To Ginny, Emma, and Ben, who are still waiting on that pool to go up, to family and friends who are wondering when my hair will stop looking so ruffled, and to special running comrades who are waiting for me to stop complaining that I'm out of shape: Thanks for understanding. Another book has been borne into the world, and we think it's an important one.
Tim adds: Thanks also to my wife, Michelle, for being so understanding
and supportive during the busy time of wedding preparations and
beginning a new life together. She has been patient and tolerant
of those evenings when I had to spend an inordinate amount of
time in the den at my computer and when my schedule got a bit
hectic. She has been a terrific partner, and I look forward to
a wonderful life together with her. Thanks also to our pet cockatiel,
Buddy Bird, who not only provided me with some great examples
in the book, but was a great companion on those lonely days when
Michelle was at work. We also thank our parents for their support
all through the years, both past and present.
About the Authors
Keith Brophy has many years of experience in the design, development, and testing of software systems. He is currently a software release coordinator for X-Rite, Incorporated, a leading worldwide provider of color and appearance quality control software and instrumentation in Grandville, Michigan. Prior to that, he was a lead software developer for IBM's System Integration and Federal Systems divisions in the Washington, D.C., area and worked on a wide variety of systems. His experience includes building Internet systems in the "pre-Web" era. During this time, he also was responsible for various operating system, performance, and graphical user interface research and development projects. He has taught in various venues, including Northern Virginia Community College and as the advanced Visual Basic adjunct faculty member at Grand Rapids Community College.
Mr. Brophy, along with Mr. Koets, co-authored Visual Basic 4 Performance Tuning and Optimization (Sams 1996) and was a contributing author for Visual Basic 4.0 Unleashed (Sams 1995). He also served as technical editor on Real-World Programming with Visual Basic (Sams 1995) and the revised edition of Teach Yourself Visual Basic 4 in 21 Days (Sams 1995). He has a B.S. in computer science from the University of Michigan in Ann Arbor and an M.S. in information systems from Strayer College in Washington, D.C. Mr. Brophy is the founder of DoubleBlaze Software Consortium (http://www.doubleblaze.com), an ActiveX Internet research and development company involved in endeavors such as research for this book.
Timothy Koets is a software engineer at X-Rite, Incorporated, a leading worldwide provider of color and appearance quality control software and instrumentation in Grandville, Michigan. Prior to this, Mr. Koets was a computer systems engineer in the Systems Engineering and Integration division of Martin Marietta in the Washington, D.C., area. In addition to developing Visual Basic applications, Mr. Koets has experience in many other areas including Visual C++, computer networking, client/server application design, parallel processing, and performance analysis. He, too, has previous experience building pre-Web systems that were Internet aware. Mr. Koets is an adjunct faculty member at Grand Rapids Community College, where he teaches advanced Visual Basic, and has prior teaching experience ranging from computer programming and engineering laboratory classes to Lotus Notes training courses.
Mr. Koets, along with Mr. Brophy, co-authored Visual Basic
4 Performance Tuning and Optimization (Sams 1996) and was
a contributing author for Visual Basic 4.0 Unleashed
(Sams 1995). He has a B.S. in electrical engineering
and an M.S. in electrical engineering from Michigan Technological
University in Houghton, Michigan. Mr. Koets is the founder of
Cockatiel Software, an Internet research and development company
that is an affiliate of DoubleBlaze Software Consortium (www.doubleblaze.com).
Tell Us What You Think!
As a reader, you are the most important critic of and commentator on our books. We value your opinion and want to know what we're doing right, what we could do better, what areas you'd like to see us publish in, and any other words of wisdom you're willing to pass our way. You can help us make strong books that meet your needs and give you the computer guidance you require.
Do you have access to CompuServe or the World Wide Web? Then check
out our CompuServe forum by typing GO
SAMS at any prompt. If you prefer the World
Wide Web, check out our site at http://www.mcp.com.
If you have a technical question about this book, call the technical support line at 800-571-5840, ext. 3668.
As the team leader of the group that created this book, I welcome your comments. You can fax, e-mail, or write me directly to let me know what you did or didn't like about this book-as well as what we can do to make our books stronger. Here's the information:
201 W. 103rd Street
Indianapolis, IN 46290
How important is VBScript to the future of computing? How important is it to you? Does it give you new capabilities that you can't get in other languages, or is it just another choice in a sea laden with confusing buzzwords? If you've pondered questions like these, you're not alone. VBScript is one of the most exciting new players in the rapidly expanding universe of technologies loosely termed the Internet. The purpose of this book is to teach you how to use VBScript. As a brief prerequisite to that journey, consider why VBScript is such an important part of the Web page development arsenal. Perhaps the best way to understand the potential of this future-centered technology is to take a look at how far the Internet has come. You might find that you have been involved in many of the trends leading up to the advent of VBScript without even realizing it, just as the authors have been.
A little over a decade ago, one of the authors was producing Department of Defense software and the reams of documentation that go along with it. One of the requirements in putting together this documentation stipulated that it should be generated in a markup language called SGML. This markup language was quite cumbersome. For example, it required that each heading start with an h1 tag and each paragraph with a p tag. Eventually, this project came to an end, and it seemed that this memory was just a relic of the past. Then, along came the World Wide Web. Behold: The World Wide Web is based on a page description language inherited to a large extent from SGML! The tag-oriented approach for Web pages succeeded precisely because it leaves the work of presenting Web pages to the browser. This approach provides an efficient, low-overhead means for communicating across the Internet because it enables the information sent across the network to be content-centered while the browser takes care of the cosmetic details on its own.
About a decade ago, another important change in the computer industry began. The mainframe-centered computing world, where all work was performed on a central computer, was quickly being replaced by a more distributed model. Users could now do some of the work locally, using the horsepower of their own pcs rather than the mainframe. For example, an accountant could now do much of her work in a spreadsheet on her own pc, rather than vie for time on an overloaded mainframe to perform accounting analyses.
Once users had more computing power in front of them, they could take advantage of that power with a graphical user environment. Windows filled this need, but the next big problem was creating the Windows applications themselves. In the early days when C was the only viable language alternative, Windows program development required such degrees of experience and development time that Windows programming was out of the reach of many. It was also quite difficult to integrate other commercial components into applications. Visual Basic came forward to fill these voids. Because of its ease of use and component integration capability, Visual Basic achieved widespread acceptance over the course of the next few years and releases. Some even claim that this language, which did not exist a decade ago, is now the world's most popular programming language.
Development for this very exciting environment required a language that not all could easily master. Sound like a familiar problem to you? It did to Microsoft, too, and in the process of inventing a comprehensive Internet strategy, Microsoft reinvented, or at least reengineered, the wheel. Visual Basic, Scripting Edition was its answer to these problems. VBScript, a subset of its parent, Visual Basic, is an easy-to-use language that is a cinch to integrate and provides an easy path to incorporate components.
If this brief history of the Internet and the reasons why VBScript came about are new to you, don't worry-you can take comfort in the fact that the currents of change are flowing in the right direction to make your Web page development easier than ever before. As to the "how" of VBScript, we'll tell you everything you need to know about using VBScript in the pages ahead.
Step-by-step, we will teach you how to use Visual Basic, Scripting Edition to its fullest potential. You will also see how to take advantage of powerful intrinsic controls, ActiveX controls, Java applets, and other objects through VBScript. During this journey, we will cut through all the Internet jargon and buzzwords, helping you to clearly understand how all the pieces of the Internet fit together and how you can participate in this new way of sharing information. We won't bore you with all kinds of information on the Internet that you don't need, nor will we give you just a cookbook of techniques. Rather, this book will give you the ability to creatively and expressively use VBScript to write impressive, powerful, useful Web pages.
Who Should Read This Book
This book is for you if you find yourself in one of the following categories:
- You've seen what's on the Web, and you want to contribute your own content.
- You represent a company that wants to make its presence known on the Internet in a powerful and productive way.
- You're an information developer, and you want to learn how the Web can help you present your information online.
- You're a Visual Basic programmer who wants to create Web pages on the Internet without learning yet another programming language.
- You've used HTML to create Web pages, but you find it too limiting.
- You've mastered HTML and CGI and want to move beyond the limited capabilities of static Web pages. You want to use VBScript to bind programs around your pages, making your Web pages more dynamic.
What This Book Contains
This book is intended to be completed in 21 days-one chapter per day-although the pace is really up to you. We have designed the book as a teacher would teach a course. We start with the basics and continue to introduce more of the language to you as the chapters progress. By the end of the first week, you will be creating your first VBScript Web page. As you work through the chapters, you will continue to design Web pages using VBScript that will become increasingly more powerful. In the second and third weeks, you will be exploring the more advanced features VBScript provides. In addition to instruction, we will furnish an abundance of examples, as well as exercises for you to try. You can learn a great deal by reading, but only when you try building a few VBScript Web pages of your own can you become truly experienced.
- During Week 1, you get a general overview of the Internet, the World Wide Web, Web browsers, and the languages available to develop Web pages. You will learn how to create variables, how to use operators to put variables to use, and the overall syntax of VBScript. Finally, you'll see a Web page incorporating useful VBScript code that will bring the Web page to life!
- During Week 2, you will be introduced to ActiveX controls. ActiveX controls enable you to present your Web page to the user in a very powerful way. You will also learn about intrinsic controls and objects. Then you will learn how to control the user interface, process and interpret user input, work with mathematical functions, and extend VBScript capabilities in various ways.
- During Week 3, you will learn about even more powerful capabilities of VBScript. You will learn more about handling dates, strings, and mathematical functions. You will be exposed to advanced concepts about ActiveX control objects and intrinsic browser objects. You'll learn about script-based strategies to generate data on the client, validate it, and then provide it to the server for storage. You'll also learn key debugging and porting skills. At the completion of this week's material, you will have the well-rounded background needed to tackle all aspects of VBScript development.
With this book, you will become comfortable enough with VBScript to design and implement powerful Web pages of your own and become confident in your abilities to use it effectively-all in three weeks or less!
What You Need Before You Start
Because this is a book about creating Web pages, we assume that you are already able to access and use the Internet. You should have a World Wide Web browser that supports VBScript, such as Microsoft's Internet Explorer 3.0, and you should be somewhat familiar with it. (Steps to obtain such a browser are provided on Day 1.) A basic knowledge of the other Internet services such as electronic mail, FTP, Telnet, Usenet news, and Gopher is not necessary for working through this book, but it may be helpful for your future page development, particularly if you want your Web pages to interact with these services. As is true whenever you learn a new programming language, the more experience you have with programming, the better. Don't worry, though, if programming is new to you; you should be able to write VBScript code with a minimal amount of previous programming experience. Our objective is to begin from the ground up, and we do not assume you are an expert programmer. After all, VBScript is not designed just for experts, but for anyone interested in building active Web pages quickly-you don't necessarily have to be a computer language guru.
You're now ready to begin the first chapter on your journey of learning how to use VBScript. We're excited about showing you the potential of this new, exciting language. The journey will be well worth the effort.