13.2. Client-Pull Documents
Client-pull documents are relatively easy to prepare. All you need to do is embed a <meta> tag in the header of your HTML or XHTML document. The special tag tells the client Netscape browser to display the current document for a specified period of time, and then load and display an entirely new one just as if the user had selected the new document from a hyperlink. (Note that currently there is no way to change just a portion of a document dynamically using client-pull.) Section 6.8.1, "The <meta> Header Element"
13.2.1. Uniquely Refreshing
You may recall from previous discussions that whenever an HTTP server sends a document to the client browser, it precedes the document's data with one or more header fields. One header field, for instance, contains a description of the document's content type, used by the browser to decide how to display the document's contents. For example, the server precedes HTML documents with the header "Content-type: text/html," whose meaning should be fairly obvious.
As we discussed at length in Chapter 6, "Links and Webs", you may add your own special fields to an HTML document's HTTP header by inserting a <meta> tag into its <head>. Section 6.8.1, "The <meta> Header Element"
The HTTP Refresh field implements client-pull dynamic HTML documents, enabled by the <meta> tag format:
<meta http-equiv="Refresh" content="field value">
The tag's http-equiv attribute tells the HTTP server to include the Refresh field, with a value specified by the content attribute (if any, carefully enclosed in quotation marks), in the string of headers it sends to the client browser just before it sends the rest of the document's content. The browser recognizes the Refresh header as the mark of a dynamic HTML document and responds accordingly, as we discuss next.
13.2.2. The Refresh Header Contents
The value of the content attribute in the special <meta> Refresh tag determines when and how the browser updates the current document. Set it to an integer, and the browser will delay that many seconds before automatically loading another document. You may set the content field value to zero, meaning no delay at all. In that case, the browser loads the next document immediately after it finishes rendering the current one, by which you may achieve some very crude animation effects. Section 184.108.40.206, "The content attribute"
220.127.116.11. Refreshing the same document
If the Refresh field's content value is the number of seconds alone, the browser reloads that same document over and over again, delaying the specified time between each cycle, until the user goes to another document or shuts down the browser.
For example, the browser will reload the following client-pull document every 15 seconds:
<html> <head> <meta http-equiv="Refresh" content="15"> <title>Kumquat Market Prices</title> </head> <body> <h3> Kumquat Market Prices</h3> Kumquats are currently trading at $1.96 per pound. </body> </html>
The financial wizards among you may have noticed that with some special software tricks on the server side, you can update the price of kumquats in the document so that it acts like a ticker-tape machine: the latest kumquat commodity price updated every 15 seconds.
18.104.22.168. Refreshing with a different document
Rather than reload the same document repeatedly, you can tell the browser to load a different document dynamically. You do so by adding that document's absolute URL after the delay time and an intervening semicolon in the <meta> tag's content attribute. For example:
<meta http-equiv="Refresh" content="15; URL=http://www.kumquat.com/next.html">
would cause the browser to retrieve the next.html document from the www.kumquat.com web server after having displayed the current document for 15 seconds.
The URL must be an absolute one, including server type and full pathname; relative URLs don't work.
22.214.171.124. Cycling among documents
Keep in mind that the effects of the Refresh <meta> tag apply only to the document in which it appears. Hence, to cycle among several documents, you must include a Refresh <meta> tag in each one. The content value for each document in the cycle must contain an absolute URL that points to the next document, with the last document pointing back to the first one to complete the cycle.
For example, the following are the <meta> tags for the headers of each in a three HTML-document cycle.
The document first.html contains:
<meta http-equiv="Refresh" content="30; URL=http://www.kumquat.com/second.html">
The document second.html contains:
<meta http-equiv="Refresh" content="30; URL=http://www.kumquat.com/third.html">
And the third.html document has in its <head> (besides other crazy ideas):
<meta http-equiv="Refresh" content="30; URL=http://www.kumquat.com/first.html">
If it is left alone, the browser will endlessly loop among the three documents at 30-second intervals.
Cycling documents make excellent attractors, catching the attention of passers-by to a web-driven kiosk, for example. Users may then navigate through the wider collection of kiosk documents by clicking hyperlinks in one of the kiosk's attractor pages and subsequent ones.
To return to the cycling set of attractors, each document in the rest of the collection should have its own Refresh fields that eventually point back to the attractor. You should specify a fairly long delay period for the nonattractor pages -- 120 to 300 seconds or more -- so that the kiosk doesn't automatically reset while a user is reading the current document. However, the delay period should be short enough so that the kiosk resets to the attractor mode in a reasonable period of time after the user finishes.
13.2.3. Pulling Non-HTML Content
Netscape's and Internet Explorer's client-pull feature is not restricted to HTML documents, although it is certainly easiest to create dynamic documents with HTML. With a bit of server-side programming, you can add a Refresh field to the HTTP header of any sort of document from audio files to images to video clips.
For example, create a real-time video feed by adding a Refresh header field in each of a sequence of images grabbed and digitized from a camera. Include a delay of zero with the URL that points to the next image, so that as quickly as the browser displays one image, it retrieves the next. Assuming that the network keeps up, the result is a crude (really crude) TV.
Since the browser clears the window before presenting each subsequent image, the resulting flicker and flash make it almost impossible to present a coherent sequence of images. This technique is more effective when presenting a series of images designed to be viewed as a slide show, where the user expects some sort of display activity between each of the images.
Perhaps a better use of the client-pull feature is with long-playing multimedia documents for which Netscape and Internet Explorer use special helper applications to display. On a multitasking computer, such as one running Unix or Windows 98, the browser downloads one document, while a helper application plays another. Combine the client-pull capabilities with that multitasking to improve multimedia document performance. Rather than wait for a single, large document like a movie or audio file to download before playing, break it into smaller segments, each automatically downloaded by the previous segment via the Refresh header. The browser will play the first segment while downloading the second, then third, then fourth, and so on.
13.2.4. Combining Refresh with Other HTTP Header Fields
You can have your client-pull dynamic documents perform some neat tricks by combining the effects of the Refresh field with other HTTP header fields. One combination in particular is most useful: Refresh with a "Redirect" field.
The Redirect field lets the server tell the browser to retrieve the requested document elsewhere at the field's accompanying URL value. The client browser automatically redirects its request to the new URL and gets the document from the new location, usually without telling the user. We retrieve redirected documents all the time and may never notice.
The most common cause for redirection is when someone moves their HTML document collection to a new directory or to a new server. As a courtesy, the webmaster programs the original host server to send an HTTP header field containing the Redirect field and new URL (without a document body) to any and all browsers that request the document from the original location. That way, the new document location is transparent to users, and they won't have to reset their browser bookmarks.
But sometimes you want the user to reset their bookmarks to the new location because the old one won't be redirecting browsers forever, perhaps because it's being taken out of service. One way to notify users of the new location is to have the redirection URL point to some HTML document other than the home page of the new collection that contains a message about the new location. Once noted, users then take a "Continue" hyperlink to the new home page location and set their bookmarks accordingly.
By combining the Redirect and Refresh fields, you can make that notification screen automatically move to the new home page. If the browser receives an HTTP header with both fields, it will honor both; it immediately fetches the redirected URL and displays it, and it sets the refresh timer and replacement URL, if specified. When the time expires, the browser retrieves the next URL -- your new home page location -- automatically.
126.96.36.199. A random URL generator
Another application for the combination of Redirect and Refresh HTTP header fields is a perpetual, random URL generator. You'll need some programming skills to create a server-side application that selects a random URL from a prepared list and outputs a Redirect field that references that URL along with a Refresh field that reinvokes the random-URL application after some delay.
When Netscape or Internet Explorer receives the complete header, it immediately loads and displays the randomly selected document specified in the Redirect field's URL. After the delay specified in the Refresh field, the browser reruns the random-URL generator on the server (as specified in the Refresh URL), and the cycle starts over. The result is an endless cycle of random URLs displayed at regular intervals.
13.2.5. Performance Considerations
Client-pull documents consume extra network resources, especially when the refresh delay is small, since each refresh may involve a completely new connection to a server. It may take a browser several seconds to contact the server and begin retrieving the document. As a result, rapid updates generally are not feasible, especially over slow network connections.
Copyright © 2002 O'Reilly & Associates. All rights reserved.