Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Schongar P.VBScript unleashed.1997

.pdf
Скачиваний:
46
Добавлен:
23.08.2013
Размер:
1.59 Mб
Скачать

<TR>

<TD><INPUT TYPE=BUTTON VALUE="Browser Name" NAME="BrowserName" >

<TD><INPUT TYPE=BUTTON VALUE="Browser Code" NAME="BrowserCodeName" >

<TD><INPUT TYPE=BUTTON VALUE="Browser Version" NAME="BrowserVersion" >

<TD><INPUT TYPE=BUTTON VALUE="Browser Agent" NAME="BrowserAgent" >

</TABLE>

<P>

<IFRAME WIDTH=550 HEIGHT=400 NAME="MyFrame" SRC="http://www.microsoft.com">

</CENTER>

</HTML>

The <IFRAME> tag inserts a so-called floating frame. A floating frame looks like a regular frame, but it can be placed anywhere on a page, like an image or any other HTML element. Floating frames are ideal for opening windows in a document, where other URLs can be displayed. In the previous listing, we placed a floating frame centered on the window and below the command buttons, where the Microsoft Web site is displayed. The document to be displayed in the floating frame is specified with the SRC attribute, which is similar to the <IMG> tag's attribute with the same name, only instead of an image, the <IFRAME> tag's SRC attribute specifies a Web site's URL.

The NavigatorObject Project

This example reads the browser's application name and uses it to decide whether to display a generic page or the same page enhanced with Internet Explorer 3.0 features. The NavigatorObject page, seen in Figure 6.6, contains the HTML description of the generic page and a few lines of VBScript code, which uses the navigator object's properties to detect the browser. The SCRIPT section of this page appears next:

<SCRIPT LANGUAGE=VBS>

If window.navigator.appname="Microsoft Internet Explorer" Then

window.document.write "<CENTER>"

window.document.write "<H1>A Web site in a floating frame</H1>"

window.document.write "<IFRAME WIDTH=500 HEIGHT=400 NAME='MyFrame' SRC='http://

www.mcp.com/sams'>"

window.document.write "< CENTER>"

End If

</SCRIPT>

If the browser can handle VBScript (if not, the browser probably can't handle floating frames and other Internet Explorer 3.0 specific features, either), it will execute the script and will display the page in a floating frame. If the browser doesn't recognize the SCRIPT section, or if it's not Internet Explorer, nothing will happen, and in effect, the entire script will be ignored. The following text shows the entire HTML document, which consists of a SCRIPT section that displays the Sams Web site in a floating frame, and the HTML description of the generic page that prompts the user to download Internet Explorer:

<HTML>

<HEAD>

<SCRIPT LANGUAGE=VBS>

If window.navigator.appname="Microsoft Internet Explorer" Then

window.document.write "<CENTER>"

window.document.write "<H1>A Web site in a floating frame</H1>"

window.document.write "<IFRAME WIDTH=500 HEIGHT=400 NAME='MyFrame' SRC='http://

www.mcp.com/sams'>"

window.document.write "< CENTER>"

End If

</SCRIPT>

<CENTER>

<H1>This site is optimized for Internet Explorer 3.0</H1>

<P>

Click <A HREF="www.microsoft.com/ie">here</A> to download the browser.

<A HREF="http://www.microsoft.com/usa/ie/rm/supernet.htm"><IMG BORDER="0" SRC="ie_animated.gif" WIDTH="88" HEIGHT="31" VSPACE="7" ALT="Microsoft Internet Explorer"

></A><BR>

</CENTER>

</HTML>

Figure 6.6 : If you view the NavigatorObject with Internet Explorer 3.0, the page displays a Web site in a floating frame.

The generic page used in this example, shown in Figure 6.7, is too generic. It doesn't display any actual content; it just prompts the viewer to download Internet Explorer and provides a link to the download site. You can open this project with other browsers to see how it works. The code for this example has a small bug; namely, it doesn't distinguish between versions 2.0 and 3.0 of Internet Explorer. Internet Explorer 2.0, for example, doesn't support frames. If you anticipate that some of your viewers still use Internet Explorer 2.0, you must also check the version of the browser from within your application to make sure it will not attempt to display floating frames using Internet Explorer 2.0.

Figure 6.7 : If the NavigatorObject page is opened with a browser other than Internet Explorer, it displays this generic page.

The location Object

The location object provides information about the window's current URL. You have already seen examples of the location object, but all of its properties have not been discussed. This section provides the complete list of the properties of the location object.

href

Returns or sets the compete URL for the location to be loaded into the browser's window. Use this property to

connect to another location through your VBScript code.

To find out the current document's URL, use a statement such as this one:

MsgBox "You are currently viewing " & document.location.href

You can also cause another document to appear on the window, or a frame, with the statement:

document.location.href="http://www.microsoft.com"

The following properties set the various parts of the URL:

protocol

Returns or sets the protocol of the URL (usually http).

host

Returns or sets the host and port of the URL. The host and port are separated with a colon, as in host:port.

hostname

Reads or sets the host of a URL, which can be either a name or an IP address.

port

Returns or sets the port of the URL (you rarely have to specify the port number in a WWW URL).

pathname

Returns or sets the pathname of the URL. Use this property when you want to specify a document, other than the Web's root document, to be displayed.

search

Returns or sets the search portion of the URL, if it exists. The search portion of the URL is the string that follows the question mark in the URL when the browser submits data to the server. If the URL is

http://www.someServer.com/cgi/RegisterUser.exe?UName="Peter Evans"

the search portion of this URL is UName="Peter Evans". If the URL doesn't contain a search portion, the search property is an empty string.

hash

Returns or sets the hash portion of the URL.

The LocationObject Project

The LocationObject page on the CD demonstrates the location object's properties. The page LocationObject has a structure similar to that of the previous examples. It contains a floating frame and a few command buttons that demonstrate the properties of the location object. The floating frame of the page shown is a container into which any HTML can be loaded and displayed. To display a new page in the floating frame, the user must click the Get me to

this URL button and is prompted to enter the desired URL. The program expects the full name of the URL, including the protocol identifier. The URL of the page is http://www.mcp.com/sams.

The second command button displays the URL of the document in the floating frame in a message box. Finally, the Location Properties button displays several of the location's properties in a message box, as shown in Figure 6.8. Notice that the root document on a Web site doesn't have a pathname property. For the pathname property to have a value, you must follow a hyperlink to another document.

Figure 6.8 : The location properties of a page on the Sams Web site as displayed from within the LocationObject page.

The actual code of the LocationObject page appears next:

<HTML>

<HEAD>

<SCRIPT LANGUAGE=VBS>

Sub GoURL_onClick()

newURL=InputBox("Enter the URL of the location you want visit")

Window.frames(0).location.href=newURL

End Sub

Sub ShowURL_onClick()

MsgBox "You are currently viewing the URL " & Window.Frames(0).location.href

End Sub

Sub LocationProps_onClick()

str="PROTOCOL " & Window.Frames(0).location.protocol

str=str+chr(10)+"HOST " & Window.Frames(0).location.host

str=str+chr(10)+"HOSTNAME " & Window.Frames(0).location.hostname

str=str+chr(10)+"PORT " & Window.Frames(0).location.port

str=str+chr(10)+"PATHNAME " & Window.Frames(0).location.pathname

str=str+chr(10)+"SEARCH " & Window.Frames(0).location.search

MsgBox str

End Sub

</SCRIPT>

<CENTER>

<TABLE WIDTH=500>

<TR>

<TD><INPUT TYPE=BUTTON VALUE="Get me to this URL" NAME="GoURL" >

<TD><INPUT TYPE=BUTTON VALUE="Show me this URL" NAME="ShowURL" >

<TD><INPUT TYPE=BUTTON VALUE="Location Properties" NAME="LocationProps" >

</TABLE>

<P>

<IFRAME WIDTH=500 HEIGHT=400 NAME="MyFrame" SRC="http://www.microsoft.com">

</CENTER>

</HTML>

The code behind the GoURL button sets the href property of the location object of the window's first frame. The code of the ShowURL button reads this property and displays it in a message box. The code behind the third button's click event becomes lengthier, as it creates step by step the string with all the properties of the location object before displaying them in the message box. chr(10) is the new-line character and is used to cause each new property to appear on a separate line.

The link Object

Another invisible object, the link object, represents a link in an HTML document and exposes various properties through which you can find out the destination of the link. The basic property of the link object is the length property, which returns the number of the links in the document. Each link is a member of the links array. The first link is links(0), the second one is links(1), and so on. Because the hyperlinks in a document are destinations, the link object's properties are identical to the properties of the location object; however, they are read-only this time. A brief explanation of each property follows, but for a more detailed discussion of these properties, see the "The location Object" section.

href

Returns or sets the compete URL for the location to be loaded into the frame.

protocol

Returns or sets the protocol of the URL (usually HTTP).

host

Returns or sets the host and port of the URL.

hostname

Reads or sets the host of a URL, which can be either a name or an IP address.

port

Returns or sets the port of the URL.

pathname

Returns or sets the pathname of the URL.

search

Returns or sets the search portion of the URL, if it exists.

hash

Returns or sets the hash portion of the URL.

target

The last property of the frames object is the target that may have been specified in the <A> frame. The target of the link is the window, or frame, where the destination document will appear.

The LinksObject Project

The LinksObject document demonstrates the use of the link object and the links array. The LinksObject page contains a floating frame, where a user-supplied URL appears, and a number of command buttons. The first three command buttons are navigational (you have seen the code behind those buttons in the previous examples of this chapter), and the other three manipulate the links and anchors arrays. The Count Hyperlinks button displays the number of hyperlinks and anchors in the document displayed in the floating frame. The other two buttons display, on a new page, all the hyperlinks and anchors on the current document, respectively.

The hyperlinks and anchors of the document appear on the same page as the LinksObject document, in effect replacing the original document.

Instead of repeating the code of the entire document, the text shows only the subroutines that count the hyperlinks and display the list of hyperlinks and anchors. The CountLinks() subroutine uses the length property of the links object to get the number of hyperlinks in the document and the length property of the anchor object to get the number of anchors. Then, it combines them in a string which is displayed with the MsgBox() function:

Sub CountLinks_onClick()

str="This document contains" & chr(10)

str=str & window.frames(0).document.Links.Length & " links and" & chr(10)

str=str & window.frames(0).document.Anchors.Length & " anchors"

MsgBox str

End Sub

The ShowLinks() and ShowAnchors() subroutines open a new document on the same window and write the elements of the links and anchors arrays, respectively, there. Notice that the links are placed on the document as hyperlinks so that you can also follow them from this page.

Sub ShowLinks_onClick()

Document.clear

Document.open

For i=0 to window.frames(0).document.Links.Length-1

Document.write "<A HREF=" & window.frames(0).document.Links(i).href & ">" & window. frames(0).document.links(i).href & "</A><BR>"

Next

Document.Close

End Sub

Sub ShowAnchors_onClick()

Document.open

For i=0 to window.frames(0).Document.Anchors.Length-1

Document.write window.frames(0).Document.Anchors(i)

Next

Document.Close

End Sub

To return to the previous window, you must click the browser's Refresh button. The Back button won't work because no previous document is in use. It's the same document! When you use the write method to send output to the current window, you're not creating a new page, you just change the contents of the page that contains the script dynamically.

Review

The Internet Explorer scripting object model is a collection of objects through which you can control Internet Explorer and the HTML documents displayed in it programmatically. As demonstrated in several examples in this chapter, it is even possible to create the content to be displayed on the fly, with the methods of certain objects of the scripting model (in particular, the document object, which is the flexible one).

The scripting model is a hierarchy of objects, each one of which exposes certain properties and methods. Some of the properties are objects, and they can apply to more than one object. The document object, for instance, is a property of the window object and represents the document currently displayed in the window. If the current document contains frames, each frame has a document property as well, which represents the document of the individual frame.

The document object provides the write method, which lets you control the actual contents of the document from within your script. When the document object is used as a property of the window object, the write method applies to the document displayed on the window, regardless of whether or not it contains frames. When the document object is used as a property of a frame, the write method applies to the specific frame.

The objects of the scripting model are arranged in a hierarchy that reflects their function and their role in scripting. The topmost object in this hierarchy is the window object, which is the container for HTML documents, forms and scripts. Most of the window's properties are objects, too, such as the frames object (a collection of all the frames in the current document, if any), the history object (an object that exposes the navigational methods, such as Back and Forward), the location object (which lets you connect to any URL via its href property), and so on. The document object is the most flexible object in the hierarchy, in the sense that it allows you, the programmer, to generate HTML pages on the fly, based on conditions such as user preferences, dates, or the browser used to display the page on the client.

Chapter 20

CGI and VBScript

by Ramesh Chandak

CONTENTS

Understanding CGI

CGI Specification

CGI Environment Variables

Architecture of a CGI Application

CGI Versus VBScript

Examples

Relevant Web Sites

Review

The very first Web pages lacked interactivity. Users could only browse through them and use hyperlinks to jump from one page to another. Common Gateway Interface (CGI) is one of several ways you can use to add interactivity to your Web pages. Other methods include using Java, JavaScript, VBScript, ActiveX, and plug-ins.

CGI represents a simple protocol of communication between the Web forms and the programs that reside on the Web server. A CGI script or program gets its input from the Web forms, processes it, and sends the results back to your browser. CGI is not a programming language; it is a script or a program that resides on the server. You can create a CGI script or program using almost any programming language, such as C/C++, Visual Basic, Perl, FORTRAN, or AppleScript, that supports standard input and output processing. This chapter introduces you to the concept of CGI, discusses why CGI was invented, and explains the different CGI environment variables. It also discusses the basic architecture of a CGI-based Web application and how data is communicated in such an architecture. This chapter further discusses the process of building a CGI application and how you can use VBScript to perform certain processing on the client side thus eliminating the need for CGI scripts or programs for some common tasks.

The CGI programs should process data and respond quickly. Remember, CGI programs get their input from the Web forms. The user is awaiting response while the data is being processed. Therefore, the CGI programs must respond quickly. Because the CGI programs reside on the server, you benefit from the server's processing power.

Early on during the design of your project consider the following two points:

Does your application demand the use of CGI?

A static, information-only Web page might not need a CGI program. An interactive Web page would probably use CGI programs.

How would you protect your CGI programs?

Because the CGI program is invoked as a result of user actions through the browser, consider necessary security precautions. The CGI programs should be under direct control of the Webmaster only; all others, including users, should have only execute rights. The users should not have delete, modify, rename, or move rights for the CGI programs.

Understanding CGI

The best way to understand CGI and what it does comes from examples. The following example demonstrates the results obtained using CGI and without CGI. In this example, an order form is created and placed online to enable customers to order products from anywhere in the world.

Listing 20.1 shows the HTML code that uses the mailto: command to send the order information from the browser to the Web server when the customer clicks the Send button.

The company, product, and price information included in Listing 20.1 is fictitious and exists for demonstration purposes. Notice the following line in Listing 20.1:

<FORM method=POST ACTION="mailto:xyz@rksoftware.com">

The preceding line indicates to the browser the information being sent to the specified e-mail address when the user clicks the Send button using the mailto: command.

Figure 20.1 shows part of the HTML page resulting from Listing 20.1.

Figure 20.1 : HTML page created using Listing 20.2's code.

Listing 20.1. HTML sample using the mailto: command for ordering software.

<html>

<head>

<title>Order Desk

</title>

</head>

<BODY background="wrbgrnd6.gif"

bgproperties="fixed">

<center>

<h1> Order Desk <a href="ita.html">

<img border=0 align="middle" src="itlogo2f.gif" alt="RK Software logo"></a>

<img src="amex.gif"> <img src="mastercd.gif"> <img src="visacard.

gif">

</h1>

<br><a href="order.html"><img src="ordpgbu7.gif" align="middle"

alt="Go to DOWNLOADABLES Order Form" border=0></a> .

<a href="customer_service.html"><img src="custsbut.gif"

border=0 align="middle" alt="Customer Servie / FAQ">

</a>

</center>

<hr>

<br><b>USE OF THIS FORM</B>

<br>

<br>

This page is for ordering software on <b>diskettes and CD-ROM</b>.

<br>

<br>Click <a href="order.html">HERE</a> to go to a page where

you can order software and <b>forms</b> which you can <b>download</ b>,

including a truncated version of <a href="abc_soft.html">ABC Software</a>

for Windows ("Personal Edition").

<br>

<br>Order on-line using this form

or email your responses to this form to XYZ Company at

<a href="mailto:xyz@rksoftware.com">xyz@rksoftware.com</a> or

call (800)555-5555 or +1(123)876-9876 or fax to (123)876-9876.<r>

<br><b>*** A 10% restocking charge will be applied to all returns

***</b>

<br>

<br>Please add shipping charges where indicated. We look forward to