One of the most powerful, yet most crippling, uses of graphics found on the Web is the image map. Image maps are considerably larger graphics than the buttons, icons, and inline images that have been discussed previously, but they give you a few more options than a row of buttons or hotlinked icons across the screen. These fancy images are like lists of hotlinks, except they are visual in nature. Up to this point I've discussed graphic images that are linked to a single site on the Web using an <IMG SRC> tag inside an <A HREF> structure. In the button scenario, you jump to a single location when you click on it. But wait! There's much more!
Image maps allow you to assign multiple links on a single graphic by defining certain areas as hot. These hot areas can be linked to, well, anywhere. Sounds cool, right? Well, there are also disadvantages to using them, but let's get a little further into the chapter first. This chapter looks at how image maps work, and it shows some examples of them in use. The chapter also discusses when they are appropriate for your pages. The code for using image maps will also be presented with a task focused on the steps to utilize the power of the visual image map.
The first part of this chapter discusses the general issues of using image maps, especially in how image maps compare with buttons, button bars, jump lists, and text menus. After this introduction, you will see a specific example of how to set up an image map.
As you browse the Web you will find many people are splashing huge image maps on their pages that are generally cool, once they download. There are definite instances when image maps are the best solution (see Figure 14.1), but of the hundreds of thousands on the Net, few were the best solution for the instance. Many of the maps could be divided into separate images and used with <TABLE> to make load times decrease. Image maps are frequently overused and many times poorly used. To understand this, let's see how they work.
Figure 14.1 : A sample image map used at a site (http://www.purdue.edu/).
Tip |
Because many image map methods require server-side resources, a combination of a background graphic and transparent table graphics can achieve the same results. Image maps are regions of your screen assigned to links. Clicking in one area of the image will take you to one location, while clicking in another will take you somewhere else. The graphic that is displayed is just an ordinary Graph-ical Interchange Format (GIF) or Joint Picture Experts Groups (JPEG) graphic, just like the buttons you created in the last chapter. However, an additional file is kept with the image called a map definition file. This is an ordinary text file that contains the definition of where the hotspots on the image are located. This information is stored as coordinate pixel locations with corresponding links. |
Tip |
In addition to the image and map definition file, an image map needs one more item to work properly: a Common Gateway Interface (CGI) script. This script is a special program that acts as a middleman between your browser and the map definition file. When you click the image map, the CGI script looks in the map definition file to see what to do. It then points the browser in the right direction, and you're on your way to another site (see Figure 14.2). |
Figure 14.2: How does an image map work?
One of the nice things about image maps is that you don't have to be a CGI programmer or know how to code the map definition file to use them. Usually the first one you create is the most difficult, but there are several tools designed to aid you in your quest.
The first difficulty is the creation of the map definition file.
Not too long ago, manually crunching the code for the map definition
would be enough to convince you to do it another way. The definition
file looks a lot like PostScript codes that your printer spits
out when it chokes on a print file.
Note |
A typical image map definition file contains several pieces of information for the server's CGI image map routine. Depending on the type of server, the nitty-gritty of the file might be a little different, but in general, the overall contents are the same. The meat and potatoes of a definition file are lines that start with words like rect, poly, and line. These are the type of areas being defined. For example, a line of map definition code that says rect /tg/department/index.html 27,20 217,56 defines a rectangular area with coordinates 27,20,217,56 that is linked to /tg/department/index.html. If you click in this area on the map, the CGI script sends you to the file. After you've created an image map, take a peek inside. Since it's a text file, you can open it into a text editor very easily. |
But never fear! The CD-ROM includes a utility for automatic generation of the map file. If you can draw some lines and know what type of server you have, you can generate a map definition file automatically. A little later, the task talks about these tools more in depth and how to use them.
The second difficulty is the CGI script itself. For the most part,
this shouldn't be a problem. Before you start biting your nails
and breaking out the CGI encyclopedia and technical manual, talk
to your site administrator. More than likely there's already a
CGI image map routine waiting for you on your server, or he will
know where to find one that's compatible with your server. Just
find out where it is and what type of server you have, and it'll
do nicely.
Tip |
Make sure you check with your system administrator to determine what type of server you have, regardless of the CGI script issue. When you create your map definition file, you'll need to know whether your server is a National Center for Supercomputing Applications (NCSA) or European Laboratory for Particle Physics (CERN) type so you can generate the correct file. |
Image maps are frequently used to visually describe the areas at a site (see Figure 14.3). This presents a unique way to engage your audience and give them an overview of your site. It may also be easier to create and link a single image map than to create, arrange, and link several small graphics with an editor and HTML code. But when should you really consider using an image map? Why not splash one on every page?
Figure 14.3 : An alternative to using an image map-rectangular inline images (http://www.adobe.com/acrobat/).
Due to their disadvantages, image maps should only be used when you cannot accomplish your visual task any other way. Many times an alternative solution to an image map is creating several inline images that butt up against one another. Even some irregularly shaped graphics can be browser-built from several transparent images (see Chapter 16, "Utilizing Inline Graphics to Your Advantage") such as those shown in Figure 14.4.
Figure 14.4 : An alternative to using an image map-transparent inline images (http://www.eded.com/-dhopkins/main.html).
Once you have overcome the hurdle of creating your first image map, you'll see that it can be tempting to use several of them, but keep in mind that image maps are slower to load than several small images that are each individually linked. This is due to the time it takes the CGI script to build the hotspots for the browser from the map definition. Image maps can also create a bottleneck at your site if several individuals try to access a single map at one time. In addition, if your site's hardware changes, so may your image map. A change may require regeneration of the graphic image and the map definition file that corresponds to it.
The HTML code to make this whole thing work is not difficult as long as you know the following:
This code is not much different from our inclusion of buttons in a page except for the use of the CGI script. Okay, time for a real example. Let's say the map definition file I created, called mymap.gif.map, is in the same place as my image. My image is in /tg and is called mymap.gif. If all this were true, my HTML coding would look like the following:
<HTML> <TITLE>home.html</TITLE> <BODY> <A HREF="http://www.site/tg/mymap.gif.map"> <IMG SRC="mymap.gif" ISMAP></a> </BODY>
</HTML>
The elements of the code are defined as follows:
This coding isn't much different from a regular clickable button except for the <A HREF> tag that specifies the map definition file. Finish the code off with the <IMG SRC> tag using the image mymap.gif with the ISMAP option for the image. This image source tag assumes that the document asking for the graphic is located in the same directory as mymap.gif. The path to this document is www.site/tg/home.html. Since I'm a graphics animal, I needed to see all this visually before describing it. You can, too, by taking a look at Figure 14.5.
Figure 14.5 : A visual example with my coding.
Once you have generated the code, move your map definition file,
image map, and calling document to your server. The biggest drawback
to image maps is that they can only be tested on the server because
of the Common Gateway Interface (CGI) script. This means that
you can't preview your image maps by running them locally on your
hard disk-a typical development technique.
Web Work Shop |
Preparing a graphic to be used as an image map usually means assembling one or more images into a single graphic. In this example, the first page shows individual components-each a separate GIF file-describing Level 1 links. The second page shows these components assembled in a single image, ready for the mapping coordinates to be applied. |
The graphic you use as the base for your image map is critical in the success of this technique. To be effective, your graphic must have clearly identifiable areas to click-some may look like buttons, others like floating icons, and others floating text. Look at Figures 6.1, 6.7, 9.1, 10.2, and 10.4 for examples of images that are appropriate for image maps. To begin creating an image map, follow these steps:
Unlike image maps, frames are a new feature included within Netscape 2.0 and Netscape Gold that are a function of the Netscape browser and don't require any external CGI scripts. Frames allow you to divide the areas of the screen into separate windows, as shown in Figure 14.6. Each window displays a separate Web page. You have complete control over how the areas are divided and can specify their area in pixels, percentages, or a relative size.
Figure 14.6 : Using frames to divide a page into regions.
The coding for frames may seem somewhat confusing at first, so let's take a look at a simple example and then a more complex one. The first thing to note about frames is that the <FRAME> tag replaces the normal <BODY> tag. In essence, the frame defining page has no body-the frames replace it. A very simple page's coding would look like the following:
<HTML> <HEAD><TITLE>A simple frame example</TITLE></HEAD> <FRAMESET ROWS="50%, 50%"> <FRAME SRC=frame1.html> <FRAME SRC=frame2.html> </FRAMESET>
</HTML>
You will see the following:
This example would have three files: the page that would include this code, frame1.html, and frame2.html. The files called frame1.html and frame2.html could have anything a normal HTML page would have-text, images, links, and so on. The frame page with simple explanatory text would look like Figure 14.7. Note again that each window is assigned to a particular file. In our case, the two windows are assigned to frame1.html and frame2.html, respectively.
Figure 14.7 : The simple frame page split into two rows.
Figure 14.8 : The simple frame page split into two columns.
Notice in the previous coding example that you could have used pixel values rather than percentages in the frameset line. Using percentages just made it easier to explain. Based on what you know about sizing images from previous chapters, you should be able to more accurately arrange your frames if need be.
Now, what if we wanted vertical frames instead of horizontal frames? Well, our coding would look like this:
<HTML> <HEAD><TITLE>A simple frame example</TITLE></HEAD> <FRAMESET COLS ="50%, 50%"> <FRAME SRC=frame3.html> <FRAME SRC=frame4.html> </FRAMESET>
</HTML>
You will see the following:
Finally, with frames you can actually divide the screen any way you want. Let's say you want to set up a 20% column on the left of the screen with a stationary logo at the bottom of it. In the other 80% column, you want a menu bar of 20% at its bottom. The coding for this page design would look like this:
<HTML> <HEAD><TITLE>A simple frame example</TITLE></HEAD> <FRAMESET COLS ="20%, 80%"> <FRAMESET ROWS ="80%, 20%"> <FRAME SRC=frame1.html> <FRAME SRC=frame2.html> </FRAMESET> <FRAMESET ROWS ="80%, 20%"> <FRAME SRC=frame3.html> <FRAME SRC=frame4.html> </FRAMESET> </FRAMESET>
</HTML>
You will see the following:
Now that you know about image maps and HTML frames, here's more:
I checked with my system administrator and we don't currently have a CGI script for image maps. Where can I go to find out how to create one? | |
Being a big Yahoo! fanatic I would suggest surfing over to http://www.yahoo.com and doing a search on CGI. More than likely you'll be able to find one that will work with your server, granted you know what you need. If you're a real code junky and want to learn CGI and write it yourself, check out some of the other books published by Sams.net, such as HTML & CGI Unleashed, which includes everything you ever wanted to know about CGI scripts. | |
I used the utility you included to generate the map definition, but the server told me it couldn't read the script. What do I do? | |
Check with your system administrator to make sure you generated the right type of script for the server. Remember that there are two different types: NCSA and CERN. Check to make sure you generated the right type. | |
I've moved all my files to the server and it's not working. What's the first thing to check? | |
First, check to make sure there are no typographic errors in your HTML code. Second, make sure you are using absolute pathnames for your references to the CGI script, map definition file, and image map. Additionally, check with your system administrator to make sure you generated the right type of map definition file. |