📄 ch28.htm
字号:
container the script is being run as part of.
<P>
The interactions with the container in which the script is running
can be on two levels with ActiveX scripting: on the container
itself or on any elements within the container's ability to control.
In the first case, you need to know the Object Model that the
container makes available to the script; that is, what are the
individual components that container has, and what is it willing
to let you use. In the second case, you need to know what other
elements could possibly be controlled, such as ActiveX controls
being downloaded, or other pieces of functionality.
<H3><A NAME="IE30BrowserObjectModel">IE 3.0 Browser Object Model</A>
</H3>
<P>
When you bring a script into Internet Explorer, you can take hold
of a variety of hidden pieces. These individual components are
treated as separate objects to the script because each has its
own unique functionality. Main windows, child windows, frames,
links, other scripts-everything is an object unto its own, and
you can isolate each object and do neat tricks with it, if you're
inclined to.
<P>
Figure 28.1 shows the basic levels of Internet Explorer's Object
Model-which object relates to which other object and how they
all piece together. Certain objects such as scripts, frames, forms,
links, and anchors may exist multiple times, with unique names
and data for each, whereas other elements have underlying components
that end up being dependent on the parent component. Forms, for
instance, start out as part of the window, but they are also part
of the HTML document. Any elements in the form itself are thus
dependent on the form component, which is dependent on the document
component, which is part of the window itself. (If you feel like
singing "the knee bone's connected to the thigh bone"
at any time, feel free.)
<P>
<A HREF="f28-1.gif" ><B>Figure 28.1 : </B><I>The Internet Explorer Object Model for ActiveX scripting.</I></A>
<P>
You can make use of this model to aim your commands accurately
at a component. If you want to aim a command at a specific checkbox
on a specific form, the chain of reference in the traditional
<TT><FONT FACE="Courier">Object.Property</FONT></TT> chain of
command would be <TT><FONT FACE="Courier">Window.Document.Form.Checkbox</FONT></TT>.
In some cases, you can bypass a level or two of this direction
because Internet Explorer is smart enough to know that if you
don't specify a window, it should just go ahead and use the base
window identifier. Many times, however, knowing how the flow of
this structure proceeds is necessary to ensure that your script
works the way you would expect, especially when you start tossing
in frames (if you're so inclined) and new windows. I show just
what you can do with the whole Object Model later in this chapter,
so you get a hands-on view of what it all does.
<P>
<CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Tip</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
Internet Explorer's Object Model is almost identical to that of the Netscape Navigator Model. Without this common format, JavaScript applications would have a tough time doing their job in IE. Besides, why change a good thing? For the most part, you can
assume that any reference to basic browser functions (windows, documents, forms, frames, and so on) are common between both Netscape Navigator and Internet Explorer.</BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<H3><A NAME="ActiveXControlsandComponents">ActiveX Controls and
Components</A></H3>
<P>
In the world of open components being obtained from anywhere,
your script may end up with more components to play around with
than you thought it had. Some basic system controls such as timers,
for example, are built in but not really part of the main container
itself. Does this mean they're off limits? Not a chance.
<P>
<CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Note</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
All ActiveX components and controls that are registered with the system are fair game for your script, with some security-based exceptions. You have access to timers, media control, database control, and other Visual Basic for Applications-capable servers,
if you know how to call them. The trick to calling them is knowing their unique identifiers, called ClassIDs or GUIDs (Globally Unique Identifiers, rhymes with squid). These piles of alphanumeric confusion come from the System Registry. By knowing what
control you want to use, you can run <TT><FONT FACE="Courier">REGEDIT</FONT></TT> and search for the name of that control, hoping you stumble across it. Knowing more about the controls themselves is also important to your design and implementation., and
you can get more of those details in <A HREF="ch27.htm" >Chapter 27</A>, "ActiveX Controls".
</BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<H3><A NAME="VBScriptObjects">VBScript Objects</A></H3>
<P>
VBScript has the option of including its own objects as well as
everything else that's built into the browser's object model.
In the initial release, the only additional object was the <TT><FONT FACE="Courier">ERR</FONT></TT>
object for getting information about runtime errors in executing
the script. Other objects will most likely appear as time goes
by, just to extend functionality while preserving the basic Object
Model.
<H2><A NAME="TheLanguages"><FONT SIZE=5 COLOR=#FF0000>The Languages</FONT></A>
</H2>
<P>
Microsoft included support for two scripting languages in its
first release of the ActiveX scripting container within Internet
Explorer 3.0: JavaScript and Visual Basic Script (VBScript or
Visual Basic Scripting Edition).
<H3><A NAME="JavaScript">JavaScript</A></H3>
<P>
The first of the HTML-embedded scripting languages, JavaScript
has a wide base of users who have begun to get familiar with its
inner workings. Recognizing that failure to include JavaScript
support would be a definite mark against the ActiveX scripting
model (and especially against Internet Explorer), JavaScript support
was included for compatibility.
<P>
There's a lot to be said about JavaScript-so much, in fact, that
one of the other chapters in this book covers it exclusively.
Because Microsoft's ActiveX scripting implementation allows the
use of regular JavaScript applications, it's better that you get
the details from <A HREF="ch24.htm" >Chapter 24</A>.
<P>
As a general rule, JavaScript and VBScript work under the same
set of limitations and use the same basic methodology for interacting
with users through the browsers and the HTML-created environment.
Although some of the function names may be different, the end
results are close to the same.
<H3><A NAME="VBScript">VBScript</A></H3>
<P>
You shouldn't be surprised that Microsoft decided to make its
own Visual Basic language the most hyped implementation of ActiveX
scripting. With hundreds of thousands, if not millions, of Visual
Basic authors in one form or another out there, doing otherwise
would have been silly. Because what you can do inside another
application over the Internet is more limited for a variety of
reasons, Microsoft scaled back the Visual Basic for Applications
specification into a more reasonable (though not quite as functional)
subset, called Visual Basic Scripting Edition (VBScript)
<P>
If you're already a Visual Basic programmer, you can probably
use VBScript in your sleep, and knowing what's in (and what's
not in) VBScript should help you get a good handle on just how
portable some of your work will be from another version of VB.
If you're not a Visual Basic programmer, the actual delineation
of what functions are and aren't in VBScript may not float your
boat, and you may want to move ahead to the examples to see the
basics of VBScript at work. In either case, you can find more
in-depth reference for what any of these commands do (or don't
do) in the VBScript documentation from Microsoft, which is available
for download at <TT><FONT FACE="Courier"><A HREF="http://www.microsoft.com/activex/">http://www.microsoft.com/activex/</A></FONT></TT>
<H4>What's in VBScript?</H4>
<P>
VBScript, which has a lot of features, is straightforward and
very functional. Although it does not have the full set of Visual
Basic functionality (see the "Resources" section for
details), it can certainly hold its own in creating eye-catching
applications. You don't need to be a Visual Basic programmer,
or really even a programmer at all, to make VBScript work or to
get a handle on some sample applications. If you become familiar
with the pieces of the whole process, writing VBScript applications
won't be any trouble.
<P>
The easiest VBScript function to come to terms with is the message
box. You've run into message boxes before; you know that they
just exist to pop up a piece of information, a warning, or an
error in a generic manner. Well, now you can make your own. In
the tradition of the "Hello, world!" application, an
HTML file with a message box in VBScript is shown in Listing 28.1.
<BR>
<HR>
<BLOCKQUOTE>
<B>Listing 28.1. A "Hello, World" VBScript sample.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"><HTML><BR>
<HEAD><BR>
<script language="vbs"><BR>
<!--<BR>
MsgBox "Hello, World"<BR>
--><BR>
</script><BR>
</HEAD><BR>
You should have seen a message box by now...<BR>
</HTML></FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
As you can see from Listing 28.1, writing basic functions is no
problem at all. If you want a message box, you use the <TT><FONT FACE="Courier">MsgBox</FONT></TT>
function. But what if you want to get a little more tricky and
make the message box more interactive? Listing 28.2 shows an example
of letting a script interact with a form on your page, where users
type in their names, and a message box appears and says "Hi"
to them.<BR>
<HR>
<BLOCKQUOTE>
<B>Listing 28.2. A "Hello, whoever you are" VBScript
example.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"><HTML><BR>
<HEAD><BR>
<script language="VBS"><BR>
<!--<BR>
Sub SayHi_OnClick<BR>
<BR>
Dim username<BR>
<BR>
username=MyForm.namebox.Value<BR>
<BR>
username="Hello, " & username
name<BR>
<BR>
MsgBox username<BR>
End Sub<BR>
--><BR>
</script><BR>
</HEAD><BR>
<form name="MyForm"><BR>
Please type in your name: <input name=namebox> <p>
<BR>
Now click this button to display a message box:<BR>
<input type="button" name="SayHi" value="Say
Hi"><BR>
</form></FONT></TT>
</BLOCKQUOTE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -