Chapter 3: Accessing Web Services from within HTML Pages

Throughout this book, you will create programs that access remote web services. As you will learn in this chapter, by using a special program called a behavior, you can provide support that an HTML-based script can use to access a web service. In other words, using only HTML, JavaScript, and a behavior, you can implement web pages that interact with a web service.

This chapter examines the steps you must perform to attach the Webservice behavior to an HTML file. Then, you will learn how to use JavaScript to interact with the behavior to call a web service’s methods. To start, you will use several of the web services that you created in Chapter 2. Then, you will learn how to use the Google web service to search for items on the web from within your own web page.

Understanding and Applying HTML Behaviors

Beginning with Internet Explorer version 5.5, Microsoft included support within their browsers for a special component called a behavior. In general, a behavior encapsulates specific functionality within a web page.

A behavior resides within a special HTML-component file that uses the .htc extension. The Webservice behavior, for example, resides in the file Each behavior should perform a specific task. The Webservice behavior, for example, provides methods that JavaScript-based code can use to interact with remote web services.

You must place the .htc file that contains the behavior within the same folder as the HTML page to which you attach the behavior. When a user later downloads the page, the browser will also download the .htc file.

Downloading the Behavior

Before you can use web services within an HTML file, you must first download the file, which contains the Webservice behavior. You can download the file from the Microsoft website shown in Figure 3.1. The Microsoft website also provides a tutorial that details the steps you must perform to use the behavior.

click to expand
Figure 3.1: You can download the file from the Microsoft website.

Attaching a Web Service to an HTML Document

Before a web page’s JavaScript code can use a behavior, you must attach the behavior to a tag within the HTML that contains the script. In addition, as discussed, you must place the behavior’s .htc file within the folder that contains the HTML file.

To attach the behavior to the HTML file, you simply reference the behavior using an id attribute that assigns a name that your code will use to reference the behavior and a style attribute that specifies the behavior’s name within an HTML tag. For example, the following <body> tag uses the style attribute to attach the behavior and the id attribute to specify a name, in this case, service, that the JavaScript code within the file can use to refer to the behavior:

<body  onload="InitializeService()"         style="behavior:url(">

. NET Web Services Solutions
.NET Web Services Solutions
ISBN: 0782141722
EAN: 2147483647
Year: 2005
Pages: 161
Authors: Kris Jamsa © 2008-2017.
If you may any questions please contact us: