📄 ch28.htm
字号:
<HTML>
<HEAD>
<TITLE>Chapter 28 -- ActiveX Scripting</TITLE>
<META>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<H1><FONT COLOR=#FF0000>Chapter 28</FONT></H1>
<H1><B><FONT SIZE=5 COLOR=#FF0000>ActiveX Scripting</FONT></B>
</H1>
<P>
<HR WIDTH="100%"></P>
<P>
<H3 ALIGN=CENTER><FONT COLOR="#000000"><FONT SIZE=+2>CONTENTS<A NAME="CONTENTS"></A>
</FONT></FONT></H3>
<UL>
<LI><A HREF="#BlendingHTMLandProgramming" >Blending HTML and Programming</A>
<UL>
<LI><A HREF="#ScriptTag" >Script Tag</A>
<LI><A HREF="#InterfaceComponents" >Interface Components</A>
</UL>
<LI><A HREF="#TheActiveXScriptingModel" >The ActiveX Scripting Model</A>
<UL>
<LI><A HREF="#IE30BrowserObjectModel" >IE 3.0 Browser Object Model</A>
<LI><A HREF="#ActiveXControlsandComponents" >ActiveX Controls and Components</A>
<LI><A HREF="#VBScriptObjects" >VBScript Objects</A>
</UL>
<LI><A HREF="#TheLanguages" >The Languages</A>
<UL>
<LI><A HREF="#JavaScript" >JavaScript</A>
<LI><A HREF="#VBScript" >VBScript</A>
</UL>
<LI><A HREF="#ScriptingHostsandEngines" >Scripting Hosts and Engines</A>
<UL>
<LI><A HREF="#Hosts" >Hosts</A>
<LI><A HREF="#Engines" >Engines</A>
</UL>
<LI><A HREF="#Resources" >Resources</A>
<UL>
<LI><A HREF="#GoingfromVBtoVBScript" >Going from VB to VBScript</A>
<LI><A HREF="#VBScriptLanguageandRuntimeReferences" >VBScript Language and Runtime References</A>
<LI><A HREF="#WebSites" >Web Sites</A>
</UL>
<LI><A HREF="#Summary" >Summary</A>
</UL>
<HR>
<P>
First, HTML pages were text. And they were good. Then they were
text with lots of fun attributes, and they looked better. Then
came images, imagemaps, audio, tables, and so on. Every time you
looked, some new tag or new functionality was being added to HTML,
but everything was still kind of static. The HTML code itself
didn't contain the content or the interactivity; the code just
pointed to it. That was then, this is now.
<P>
ActiveX scripting is Microsoft's answer to putting the programming
language inside the HTML code itself. No longer do basic interactions
such as verifying input format have to take place on the server
side-all the work can be done on an end user's machine, putting
a fraction of the power of distributed computing at your disposal.
<P>
Embedding the programming language of your choice in an HTML file
can be very useful. Embedding is not quite the way to write your
next generation of applications, because everyone can easily view,
copy, and modify the source code, but it makes more advanced interaction
easy. In this chapter, you look at the ActiveX scripting model
and become familiar with the following:
<UL>
<LI><FONT COLOR=#000000>Blending HTML and programming</FONT>
<LI><FONT COLOR=#000000>The ActiveX scripting model</FONT>
<LI><FONT COLOR=#000000>The languages</FONT>
<LI><FONT COLOR=#000000>VBScript</FONT>
<LI><FONT COLOR=#000000>JavaScript</FONT>
<LI><FONT COLOR=#000000>Resources</FONT>
</UL>
<P>
You don't have to be a programmer to get basic and useful functionality
out of ActiveX scripting; you just have to know a little background
and be willing to try it.
<H2><A NAME="BlendingHTMLandProgramming"><FONT SIZE=5 COLOR=#FF0000>Blending
HTML and Programming</FONT></A></H2>
<P>
For the better part of its reasonably young life, HTML was static.
It was originally designed around the idea of text being the information
conveyed, and it did its job quite well. Then images popped in,
as was inevitable, and soon more and more capabilities were included
as people said, "This is great, but it'd be even better if
I could present my information in this other way."
<P>
Although you could program on the server side, the desire for
distributing the work load to client machines and making it more
seamless pushed new ideas of what comprised interaction in HTML
right to the forefront. The reason is that people are impatient.
You, me, your neighbor-when we're looking for information, we
want to get it quickly. We don't want to call and get a busy signal;
we want an answer. We don't want to fill out a form, wait a minute
or two for the server to think about doing something with it,
and then get back a message saying "Sorry, you didn't complete
the form correctly." Likewise, if we're doing a job in which
we just need simple functionality-like creating basic calculations,
working with random numbers, or developing better interactions-sending
simple tasks to an overloaded computer just isn't the best solution.
When the work is done by our machine, without our ever having
to go out and connect to another location, we are saved the time,
effort, and frustration. And that savings helps us out.
<P>
Like the rest of HTML, though, editing and viewing what's going
on in the program should be easy, which means that the HTML can't
really be precompiled. It has to be a script that gets interpreted
at runtime on the client's machine. There has to be some way for
the end user's machine to realize that it has to do something.
That's where the <TT><FONT FACE="Courier">SCRIPT</FONT></TT> tag
comes in.
<H3><A NAME="ScriptTag">Script Tag</A></H3>
<P>
Starting with JavaScript, a new HTML tag was added to separate
elements that contained scripts to be interpreted and run. Because
it wasn't already taken, <TT><FONT FACE="Courier">SCRIPT</FONT></TT>
was the perfect choice. Just like other tags, you have a <TT><FONT FACE="Courier"><SCRIPT></FONT></TT>
tag to signal the beginning and a <TT><FONT FACE="Courier"></SCRIPT></FONT></TT>
tag to signal the end. You put what's supposed to be considered
a script in between those tags.
<P>
But what happens if the browser doesn't support scripts? Normally,
it would get a whole lot of garbage between the <TT><FONT FACE="Courier"><SCRIPT></FONT></TT>
and <TT><FONT FACE="Courier"></SCRIPT></FONT></TT> tags,
because the browser would just ignore those tags and move on to
the text. The answer to this problem is to trick the browser.
You place everything that's supposed to be code inside comment
tags within the <TT><FONT FACE="Courier">SCRIPT</FONT></TT> tags.
If the browser can't handle the <TT><FONT FACE="Courier">SCRIPT</FONT></TT>
tags, it just sees all that code as nondisplaying comments and
doesn't show it to the end user.
<P>
<CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Tip</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
Hiding script elements within comment tags is a trick you can use with browsers to seamlessly incorporate the code. In early 3.0 alpha and beta releases, Internet Explorer didn't handle this functionality, but later versions are designed to do so. If you
see the code but know the browser is supposed to handle the script, check your HTML and then check what version of the browser you're running.</BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<P>
What goes between the two <TT><FONT FACE="Courier">SCRIPT</FONT></TT>
tags? Almost anything. The most basic element you need to include
is an identifier for what scripting language you're using, like
the following beginning <TT><FONT FACE="Courier">SCRIPT</FONT></TT>
tag for a VBScript:
<BLOCKQUOTE>
<TT><FONT FACE="Courier"><SCRIPT LANGUAGE="VBS"></FONT></TT>
</BLOCKQUOTE>
<P>
You can also include a unique name for the script so that you
can run multiple scripts on a page and do specific jobs with different
scripts. To include the name for the script, just add it to the
first <TT><FONT FACE="Courier">SCRIPT</FONT></TT> tag, as follows:
<BLOCKQUOTE>
<TT><FONT FACE="Courier"><SCRIPT LANGUAGE="VBS" NAME="myscript1"></FONT></TT>
</BLOCKQUOTE>
<P>
As time goes on, scripting languages may add more tags to control
behavior further, but because you can control the behavior in
the script as well, adding new tags is not a real priority.
<P>
<CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Tip</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
To make sure that scripts are read in at the right time, you should normally place them in the <TT><FONT FACE="Courier">HEAD</FONT></TT> section of the HTML file. This way, you ensure that they get loaded properly.
</BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<H3><A NAME="InterfaceComponents">Interface Components</A></H3>
<P>
To show how cooperative user interfaces can be with HTML, as well
as how dependent they can be, the primary user interfaces for
scripts are the same elements that are used by HTML forms: checkboxes,
radio buttons, input fields, buttons, and so on. Without them,
most scripts are limited to tossing up message boxes and maybe
a single box in which the users can type some information. Without
the ability to use the HTML forms elements, message boxes and
input boxes by themselves would not blend well with HTML.
<P>
Because each element on a form can be named, you can use these
names to cause events and interpret them. If you have a button
named Go, for example, you can include a line in your script to
perform a specific action when someone clicks the Go button. If
you have an input box named MyText, you can get text from that
box as easily as you can send text to it. Using this capability,
you can add functionality to your forms and other functions, such
as checking for valid e-mail addresses before sending in a form
or comparing phone numbers to states to determine what sales office's
page they should get directed to for further information.
<P>
The components that are available to script functions go beyond
just HTML-created components, though; they include aspects of
the browser itself. Each part of the browser-the window, any frames,
the status bar, the menu bar-is part of the scripting environment
and can be controlled. Because each part has its own special way
of being named, you can single it out to create an effect on it.
If you want to set the status bar to read "Today's specials
are…" or create a new browser window, you can do so.
You just have to know how scripting-ActiveX scripting in particular-enables
you do make these changes.
<H2><A NAME="TheActiveXScriptingModel"><FONT SIZE=5 COLOR=#FF0000>The
ActiveX Scripting Model</FONT></A></H2>
<P>
The whole concept of ActiveX scripting is that you're using small,
interoperable components that work together to get the job done.
A browser or other application works like this as well. Applications
have child windows, objects, and sets of components that they
pass messages between them to make everything work. When you bring
ActiveX scripting into the equation, what really makes the application
more powerful is the capability to interact with the <I>parent
object, </I>such as Internet Explorer, another browser, or whatever
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -