Because it is a scripting language, you do not need a special program with which to write the script. Scripting languages can and are written as text files. The browser reads the script and executes the functions it finds there.
The browser is on the alert constantly watching for events. When the browser detects an event, it looks to see if there's an event handler that contains a series of instructions for handling that event. Those instructions are the action (a window popping open, a sound playing, and so forth.)
An action is made up of one or more scripting statements that are do statements, imperatives that command do this, do that, then do the other thing! When an action might get performed more than once on a page, it's more efficient to combine all of those imperative statements into a single unit called a function. A function is a recipe for action. When there are functions are involved, the event handler doesn't have to say "do this, do that, do the other thing." The event handler can simply so, "go find this function and perform the actions it specifies."
When Dreamweaver writes a behavior, the action part of the behavior is put into what is called a function. The event part of the behavior is added to your page as an event handler containing a function call (which contains all the instructions that will be executed when the event happens) that calls up that function. We say the event handler (the event part) calls that function (the action part). Calling the function makes the function execute and (voila!) the action happens.
For example, if someone tells you to run around the yard three times and do a cartwheel, it's comparable to a set of statements that will happen the moment the browser sees them. Just like you instantly running around, the browser performes the commands as soon as it loads the page reads this instruction.
But if someone tells you to run around the yard three times and do a cartwheel, but not until he calls your name and says go, that's a function. When he actually calls your name and says Go!, that's the function call. What do you do in response to the function call? You run around the yard three times and do a cartwheel, which is the action. When the function is called (by the function call), the browser it performs its action.
So, in terms of the code, imagine you have a simple text link in your document (like the one shown in Figure 16.1), and you add a simple behavior like the Popup Message behavior to it. Dreamweaver adds the following code to the <body> of your document (new code shown in bold):
The actual function that is run is in the <script> tag in the head of the page. It looks like this:
Other examples of actions are:
Checking to see what browser is being used
Changing any element (image, text) on a page
Going to a different page
Actions are always things only the computer does (noninteractive).
Other examples of events include:
Moving the mouse over something
Events are things the computer and the user do together (interactive).
Behaviors can add functionality to your page that goes well beyond basic HTML. Some of these behaviors are popular and used quite often. Others are little things that will give your site a professional touch. Understanding events and how they influence the usability of your page is key to making your page work the way you want and the way the user expects.
In this section, we discuss how to add behaviors to page elements and how the elements that are chosen affect the behavior options.
The Behaviors panel is the one place where the behaviors can be added, subtracted, and manipulated (see Figure 16.2). To open the Behaviors panel, go to Window > Behaviors or press Shift+F3 (Windows) or Cmd+F3 (Mac).
The plus (+) button is used to attach behaviors from a list of available behaviors for the selected element. You can attach more than one behavior to an element. The up and down arrow buttons control the order in which the behaviors are stacked. Alphabetically, events determine the order of behaviors. The up/down only changes the order of behaviors if more than one behavior uses a single event.
To attach a behavior to a page element:
Select the page element.
With the element selected, open the Behaviors panel.
Click the (+) button and select a behavior. Note that the element to which you are attaching the behavior will determine what behaviors are available. Also, page content will determine some of the options. The Timeline behaviors will only be active when there is a timeline animation on the page.
Complete the chosen behavior's dialog box and click OK. The behavior is now attached to the page element. Notice that the behavior is listed in the panel when that element is selected.
Highlight the behavior in the panel, and choose the event drop-down arrow to select an appropriate event for the behavior.
As mentioned previously, you need to remember two very important points when using behaviors:
The event options depend on the browser setting. There are events for the following:
3.0 and later
4.0 and later
IE 3 to 6 (individual browser versions)
Netscape 3 to 6 (individual browser versions)
The number of events available goes up as the browser version goes up. It is advisable that you choose 4.0 and above unless you know you have a targeted audience and you need the advanced functionality of a more specific setting. Click the plus (+) button and go to Show Events For (see Figure 16.3).
The behaviors and events available also depend on the element to which you are attaching the behavior. The list of behaviors available will differ significantly if you are attaching the behavior to the body of the page, an image, or a hyperlink around an image or text.
Here's a common example of how these two ideas work. You type some text in the Document window. Then looking to attach a behavior to it, you open the Behaviors panel and click the plus (+) button. You select the Popup Message behavior, type in your message, and click OK. Now back in the Behaviors panel, you see your new behavior. The event is set to onLoad. You don't want that one, so you click the arrow to select your events. There are only two: onLoad and onUnload. That doesn't seem right. You want onClick. Then you realize that it must be the browser setting. Ah! It was set to 3.0 and later. You change it to 4.0 and later and all will be well. You click the arrow to select the onClick event. However, now there are only four more and none is the one you want. You realize that they are all noninteractive events. onLoad, onFocus something else must be wrong.
Because you are in Code and Design view, you check the code. Ah! You notice that the call to the MM_popupMsg function is set to onLoad and written to the <body> tag! You didn't apply it to a link! So you delete the behavior again, make the text into a null link, attach the behavior to the link, go to Events, and there it is: nine interactive events. You select onClick and all is well. For fun, you change the Show Events For setting to IE 5, and you now have more than 30 events from which to choose.
So keep an eye on your browser settings and the elements to which you are attaching behaviors.
Exercise 16.1 Setting the Browser Events Level
In this exercise, you learn how the browser setting influences your choices for events.
Open a new document (File > New > HTML) and type behavior test in the Design window.
Open the Behaviors panel.
Select the word Test.
Click the plus (+) button and select Popup Message.
In the dialog that opens, type in a message and click OK to close the dialog box.
In the Behaviors panel, click the down arrow in the Events field and notice your selections.
Click the plus (+) button again and go to Show Events For. If it is set for 3.0 and later, set it to 4.0 and later. If set for 4.0, set to 3.0. Notice the change in available events.
Highlight the linked text and apply this behavior to it. Notice the difference in event options.
Change the Show Events For to IE 5.0 and notice all the events that are now available.
Set to onClick and then Preview in Browser (F12). Click the link to activate the pop-up message.
Some behaviors are content-dependent. The Drag Layer behavior will be active only if there is a layer on the page. Control Flash or Shockwave will be active only if you have a Flash or Shockwave movie on the page. The Validate Form behavior will work only if there is a form on the page.
In some instances, you will want to attach the behavior to the body. Some behaviors are designed specifically for the <body> tag.
The following behaviors are usually attached to the <body> tag:
The following behaviors can be attached to the <body> tag:
Go To URL
Open Browser Window
Set Text of Status Bar
For instance, if you want a small window to pop up when people come to your site, you can set the Open Browser Window behavior to activate when the page loads (onLoad event). This way, the browser window will open without any user interaction.
To attach a behavior to the body, you must make sure that the body tag has focus or is selected. You can do this in two ways:
Go to the tag selector at the bottom-left corner of the page and click on the body tag.
Click on an empty part of the page, making sure that no other page element is selected. You can then add a behavior, and it will be written to the body tag.
Behaviors can be added to images. Unlike adding behaviors to plain text, which will instead look to a containing element to hold the behavior (the <a> tag usually), available behaviors can be added directly to an image. At this stage, it is important to realize that there is a difference between an image and an image that is a hyperlink. You can certainly add behaviors to images, but it might be better to make the image into a link. One benefit of attaching behaviors to links is that the cursor changes to a hand over links. This gives users a visual indication that something will happen if they click. If you add a behavior to a plain, non-linked image, the behavior will still work but the user won't know that it is a clickable item. You might also run into browser concerns as not all browsers know how to handle event handlers attached to regular images. There is better browser support for behaviors within links.
Some behavior-event combinations will add a link tag when applied. This is because the link tag is required for that particular event. You can determine these events because they are within parentheses. For example, if you attach a Popup Message behavior to a regular image, by default the (onClick) event is chosen. A check of the code will show that an <a> tag has been added and the behavior is attached to the <a> rather than the image tag, which was the element selected when you added the behavior. The following exercise will demonstrate the difference between these ideas.
Exercise 16.2 Attaching a Behavior to an Image
In this exercise, we will attach a behavior to an image. Specifically, we will attach a Popup Menu behavior to an image.
Before you start, copy the chapter_16 folder on the CD to your hard drive. Define a site called Chapter 16, with this folder as the local root folder.
Open pop_up.htm from the chapter_16 folder on the CD.
Click the image to highlight it and go to the Behaviors panel (Windows > Behaviors).
Click the plus (+) button and select Popup Message.
In the dialog box, type in Window in Venice and click OK.
In the Events field of the Behaviors panel, click the down arrow and choose onClick.
If this is not available, click the plus (+) button and under Show Events For, choose 4.0 and later. onClick should now be available.
Preview in Browser (F12) and click in the image. The pop-up message should appear.
Keep this Dreamweaver window open because you will go back to it in the next section.
Now what's wrong with this picture? The behavior works fine, but your users will have no visual indication that clicking this image will actually do anything! They need some visual clue that this is an interactive element (perhaps the cursor changing as if it were a link, for instance).
You might have noticed that there are many events available for your behaviors. Some of them seem redundant, such as onClick and (onClick). These are quite different in the code that is generated and how the browser reacts to it.
Those events without parentheses will be added directly to the selected element. While this might work fine in some browsers, there will be no visual indication that the item is clickable, as it is not a link.
Events within parentheses will wrap the selected element within an <a> tag. This will do two things:
Change the cursor into a hand so that users know that it is clickable.
There is better support for behaviors added to the <a> tag.
We will now change the file we just created so that it is wrapped within the <a> tag.
Back in the page you just built, click the image and remove the behavior.
Click in the image to highlight it.
Go to the Behaviors panel and apply the Popup Message behavior.
For an event, choose (onClick) (the one within parentheses).
Preview in Browser (F12). Notice that now, when you mouse over the image, the cursor changes to a hand, indicating that the image is clickable. Go to the code and notice that Dreamweaver has automatically added an <A> tag to the image.
This defines, again, the difference and importance of knowing where in the code the behavior is attached and how this affects the functionality of your page.
Just for fun, go back to the behavior and change the Show Events For to IE 5 and look at the sheer number of events available to you.
How you integrate dynamic sources into your behaviors is up to your own imagination. Any behavior that causes a file to open will let you choose a data source for the file's URL. This includes Open Browser Window, Go To URL, Show/Hide Popup Menu, and even Jump Menus. Behaviors that show images, such as Swap Image, will let you supply a dynamic src for the image chosen. Behaviors that show text messages, such as Popup Message, Set Text of Layer, and Set Text of Status Bar, can be configured to show dynamic text elements instead of static text. The following section shows how you can be sneaky, if needed, to get your dynamic behavior assembled.
You cannot assign a dynamic text source to this behavior as you're creating it, but you can be sneaky and add dynamic text after the fact. Do this:
Apply the Popup Message behavior as normal, entering some brief temporary text to be displayed in the message.
Go to Code view or Code and Design view, and find the code for the behavior's function call.
Select and delete the temporary text you entered when you created the behavior.
From the Bindings panel, choose a Recordset field, and drag it into the Code window, exactly where you deleted the temporary text (see Figure 16.4).
A dynamic jump menu is simply a cross between a standard jump menu and a dynamic list/menu form item. Create it like this:
Create a jump menu as you normally would (Insert > Form Objects > Jump Menu), but in the dialog box that appears, assign it no entries.
In the Server Behaviors panel, click the plus (+) and choose Dynamic Elements > List/Menu. When the dialog box appears, choose your jump menu as the list/menu to affect.
Assign each menu entry a name and value from your recordset. Remember that the value must come from a field that creates a valid URL.
This section discusses some of the popular behaviors that come with Dreamweaver. Keep in mind that some of the element-specific behaviors are covered in other chapters. For instance, the behavior that deals with Form Validation is covered in Chapter 11, "Working with Forms."
Some behaviors, such as the Popup Message and the Open Browser Window, have already been discussed in examples throughout the book.
Dreamweaver can now create its own pop-up menus. This functionality was originally available in Macromedia Fireworks 4. Because it was the first implementation of this feature, it had some limitations, such as menu placement. Dreamweaver MX solves this issue, and an improved interface has been added.
To use this behavior, you need to attach it to an image or a hyperlink. Insert an image on your page. Select the image and then open your Behaviors panel. Select Show Pop Up Menu. Then follow these steps:
On the opening screen, enter the name of the menu item. This is the text that will appear on the button. Then assign the text a link by browsing to another page or entering in an absolute URL. You can set the target here if you are using Frames.
Click the plus (+) button to add this menu item. Click the Indent Item button if you want to create a submenu off the next higher menu item. Items will continue to be indented until you click the Outdent Item button. Use the up and down arrow buttons to arrange the order in which the menu items will appear.
When you have completed adding your menu items, go to the Appearance tab. Here you will determine the look of your menus.
Select whether this will be a vertical or horizontal menu. The Preview area will give you an approximate look of the menu. Choose the font name, size, and style of the menu text. Choose a justification setting. Choose the text and cell colors for the Up and Over states.
When set there, go to the Advanced tab. Here you can specify specific settings of your menu. As there are really tables, you can set cell width and height, padding and spacing, and delay time. You can also specify borders and specific colors of the borders. (These settings might be browser-dependent.)
6. Now go to the Position tab. This tab lets you specify the position of the pop-up menus relative to the image to which they are attached. If you wish, you can specify any pixel number for the X and Y values.
Click OK and your pop-up menu will be built. It will not show up in the Design window, so Preview in Browser to see it in action!
Exercise 16.3 Building a Pop-Up Menu
In this exercise, we will create a pop-up menu using Dreamweaver's built in behavior. This is a very popular way to quickly build an advanced navigation system.
If you haven't done so yet, copy the chapter_16 folder on the CD to your hard drive. Define a site called Chapter 16, with this folder as the local root folder.
Open a new HTML document and save it as pop_up.htm.
Insert the navigation.gif image (from the chapter_16 folder on the CD) onto the page. Make sure that it is selected on the page.
In the Behaviors panel, click the plus (+) button and select Show Pop Up Menu. The wizard shown in Figure 16.5 will display.
In the contents panel, type in Macromedia in the Text field. In the Link field, type in http://www.macromedia.com.
Click the plus (+) button. Type in Dreamweaver. In the Link Field, type in http://www.macromedia.com.
Click the plus (+) button again and type in Page 3. In the Link field, click the Browse folder and browse to swap_image.htm in the chapter_16 folder.
Click on the Indent Item button. This will indent Page 3 relative to "Dreamweaver". This will create another level of pop-up menu.
Click on the Appearance tab. This panel is where you determine the look of the menu.
You can choose between Vertical Menu and Horizontal Menu. Look at both options but choose Vertical Menu. Leave the rest of the default options.
Go to the Advanced tab. Leave the defaults here, but notice that these options are equivalent to table settings. Dreamweaver is actually creating tables to make these menus.
Go to the Position tab. This menu allows you to set the position of the menu relative to the image to which this behavior is attached. This is a nice improvement from the Fireworks 4 implementation of this feature!
Click on the leftmost button. This will make the menu appear from the lower-right corner of the image.
The Hide Menu on onMouseOut Event option should be checked. This will make the menu disappear when you move off it, and will add a Hide Pop Up Menu behavior to your Behaviors list. This behavior is only used in conjunction with a pop-up menu and should only be added with this check box.
Click OK to create the menu. It will not be functional in the Design window.
Press F12 to Preview in Browser and test your menu!
You can use this behavior to open a new window of the browser. This differs from setting the target to _blank (see Chapter 12, "Designing Frame-Based Pages") because with this behavior, you can define the size of the window, you can give a name to the window, and you can dictate the appearance of the window, such as what menu bars it gets, whether it gets a status bar, and so on. Setting the target does not produce this kind of control.
To use the Open Browser Window behavior, do this:
Create two files: One will be the main page that contains the behavior that opens the new window; the other document will appear in the new window.
With the main page open, select whatever page element you want to trigger the behavior.
In the Behaviors panel, click + and choose Open Browser Window.
In the dialog box that appears, enter the specifics for the new window. This includes the URL of the second page you constructed in Step 1, and various options for the window.
Name your new browser window.
To test, Preview in Browser and click in the link.
Exercise 16.4 Using the Open Browser Window Behavior
The Open Browser Window behavior (see Figure 16.6) is used quite a bit in web design. It can be used for special messages that need to get attention. It can be used if you have a large page that shouldn't have to be reloaded every time the user goes away from it. For example, if you have a page with lots of image thumbnails, you can use this behavior to open a new window with the full-size image. Then the user would not have to reload the thumbnail page when they want to see a different photo.
In this exercise, we will use the Open Browser Window behavior. With this behavior, you can set the size of the window and control what options it gets, such as scrollbars and a status bar.
If you haven't done so yet, copy the chapter_16 folder on the CD to your hard drive. Define a site called Chapter 16, with this folder as the local root folder.
Highlight this link text and then click the + sign button in the Behaviors panel and select Open Browser Window. The Open Browser Window dialog box is shown in Figure 16.6.
Enter these settings:
URL to Display: Click the Browse button and select the browser_window.htm file from the local root folder or any other page you want.
Window Width: 300
Window Height: 200
Select Scrollbars as Needed
Window Name: New
Click OK to finish the behavior.
Preview in Browser and click the link. The new window should open with the attributes you assigned.
Extensions that offer other options are available for opening a browser window. Check out the table at the end of this chapter for examples. Extensions are discussed in Chapter 35, "Working with Extensions."
This behavior sets the text in the status bar at the bottom of the browser window. This behavior gives your page that little touch that would appear to be difficult but is made easy with Dreamweaver MX.
To use this behavior, open a page and click the plus (+) button on the Behaviors panel. Choose Set Text > Set Text of Status Bar. In the dialog box that opens, type in some text. Click OK.
Since we didn't select anything, it will be added to the body tag with an onLoad event. This means that it will show as soon as the page loads. Preview the page, and the text you typed should show up in the status bar. Keep in mind that the status bar is an option in most browsers, so do not put critical information in this behavior. It's more of a nice detail.
Exercise 16.5 Using the Set Text of Status Bar Behavior
This behavior adds a little professional touch to your web page.
In an open document, type Status Bar.
Go to the Behaviors panel, and click the plus (+) button. Go to Set Text > Set Text for Status Bar.
In the dialog box, type in This behavior is easy!! and click OK.
In the Behaviors panel, make sure the event is set for onMouseOver. If this option is not available, check your Show Events For setting and make sure it is set for 4.0 and later.
Preview in Browser. Roll the cursor over the link and notice that in the lower left of the browser, your text now appears. After you mouse over that link, the text is there to stay until you leave the page or the user activates another instance of this behavior.
Let's smooth out the functionality of this behavior. Back in the Dreamweaver window, highlight the text link. The applied behavior will show in the Behaviors panel.
Click the plus (+) button and select the Set Text of Status Bar again. This time, leave the field blank and click OK.
Back in the panel, change the event to onMouseOut.
Preview in IE.
Notice that now when you move the mouse off the link, the text disappears. Although this is a cleaner design, you should certainly use this functionality however you need.
You can set up your site to show different content depending on the browser the user is using. Although this might increase the number of pages in your site, you can be assured that they will be viewed as you designed them without browser-incompatibility concerns.
To use this behavior, you are deciding a few things. The behavior decides among IE, Netscape, and others. For testing this behavior, you will need at least three browsers installed. Also, you will need to make pages that let you know that it is working. For instance, you will need to make a page that says This is Netscape or This is IE and this is the other page.
You have three choices for redirection:
Stay On This Page
Using these combinations, you can set up your redirection. You also have browser versions to consider. By default, they are set to 4.0. So the behavior is asking, "What browser is it?" but also "What version?". So those with version 3 browsers can be sent somewhere else.
For this example, we will set up the behavior so that the other browser will stay on the same page, and IE and Netscape users will be redirected. Again, you will need at least three different browsers installed to really see this. For this requirement, you can have a combination like Netscape 3 and 6, and IE 4 or above.
For URL, browse to the page you have created for Netscape 4 and above users. For Alt URL, choose the page you created for IE 4 and over users. All others are going to stay on this page.
For Netscape 4.0 or later, choose Go to URL. For Otherwise, choose Stay on This Page.
For Internet Explorer 4.0 and later, choose Go To Alt URL and for Otherwise, choose Stay on This Page.
For Other browsers, choose Stay on This Page.
Save and Preview in the various browsers to confirm that everything is working as planned.
The accompanying CD contains an example page set up to detect the browser version and then redirect accordingly. Go to the chapter_16/samples folder and open browser_check.htm directly in the browser. It should show text according to the browser type.
You can use the browser version number to further refine the behavior. You can have extended functionality in pages designed for IE 5.0 and higher. If someone comes in with IE 4.0, you can send her to less-complicated pages. This might be useful if you are using other behaviors that use 5.0 events. You can redesign these behaviors to work with 4.0 events for these viewers.
Keep in mind that this behavior is not foolproof. As with every stage of development, make sure to test all your functionality as you go.
One of the more popular behaviors used on many sites is the capability to change or swap images depending on user interaction. Dreamweaver has a built-in way to make a rollover image, as it's often called. This is when one image is swapped with another image when you roll over it. You can use behaviors to extend this functionality, creating advanced rollovers.
Basic rollovers can be created quickly in Dreamweaver, using the Image Rollover object (see Chapter 6, "Links and Navigation," for more on this.) In this section, you will learn how to make more complex rollovers.
Actually, two behaviors are used when making a rollover image:
Swap Image Restore
These are two separate behaviors that enable you to control exactly when you want things to appear and change. This is the same idea as in the status bar text example: one behavior to show it, and another to make it disappear. By default, when you add the Swap Image behavior, the Restore Images onMouseOut option is checked, and this option writes the Swap Image Restore behavior.
To make a simple rollover, click Insert > Interactive Images > Rollover Image. You will be presented with the Insert Rollover Image dialog box (see Figure 16.7).
For Original Image, browse to the image that you want to initially show.
For Rollover Image, browse to the image you want to show on the event. You can give the image Alternate text if you wish. This is for those who cannot view images. It also shows up when you pause your mouse over the image.
If you want this rollover image to be a link, go to When Clicked, Go to URL field and browse to the desired file. Click OK.
A rollover image is really a Swap Image behavior with a Swap Image Restore behavior. If you click on the image, you can see these in the Behaviors panel. By default, the event is set to onMouseOver.
Preview in Browser to see this in action. Simply roll your mouse over the image and it will swap.
These behaviors can be combined in many ways, and more than one behavior can be added to any image for more complex interaction.
Using combinations of these behaviors, you can have an image on one side of the page swap an image on the other side of the page. This is called a disjointed rollover.
An image can be set to swap itself and other images on the page with one event. This is called a multiple-image swap. Exercise 16.6 demonstrates a multiple-image swap.
To make a multiple-image swap:
Insert two images onto the page. Save it. For this behavior, it is important to name them. For each image, type in a name in the top-left field of the Property inspector.
Click on the first image and then go to the Behaviors panel. Click the plus (+) button and choose Swap Image. In the Swap Image dialog box (see Figure 16.8), your two named images should be listed.
Make sure the first image is selected, and in the Set Source To: field, browse to the image you want to be the rollover image. Leave the two boxes checked at the bottom. Now, click the second image. In the Set Source To: field, browse to the image that you want the second image to swap with.
Click OK to close the dialog box. The critical idea for this is that we just set two different images to swap when you roll over the first image, because we applied the behavior to that first image.
Preview in Browser. Roll over the first image. Both images should swap. Now roll over the second image. Nothing happens because there is no behavior applied to that image. The behavior is applied to the first image but controls the second. That is the essence of a multiple-image swap (also called a disjointed rollover).
Exercise 16.6 Creating a Rollover Image
In this exercise, you first make a rollover image the easy way and see how it was created. Then you add complexity to it.
If you haven't done so yet, copy the chapter_16 folder on the CD to your hard drive. Define a site called Chapter 16, with this folder as the local root folder.
In a new document, go to Insert > Interactive Images > Rollover Image. The Insert Rollover Image dialog box will display (refer to Figure 16.7).
In Image Name, give it a name that describes the content. Here, call it Pisa.
For Original Image, browse to the chapter_16/samples/images folder on the accompanying CD. Choose pisa1.jpg.
For Rollover Image, choose pisa2.jpg.
Leave Preload Rollover Image checked. Leave the Go to URL field empty. You can use it to navigate to another page, but for the example purposes here, you can leave it empty.
Click OK to close the dialog box.
Preview in Browser and roll your mouse over the image. It should immediately swap to the second image. Mouse off of it and it will revert to the first image.
Now go back to Dreamweaver and see what it built for you.
Open the Behaviors panel. In the Design view, click the image.
Notice that the list contains two behaviors. Click in an empty part of the Design window or click the body tag in the tag selector. Notice that the Preload Images behavior is listed. You added this via the check box in the initial dialog box. If you didn't have this behavior, the image would not be downloaded until you moused over the image. This would introduce a lag time into the swap. This behavior brings down the image as the rest of the page loads, and it is then ready for the swap.
Click again on the image. Double-click the Swap Image behavior in the panel. It will open the dialog box shown in Figure 16.8.
The Swap Image Restore behavior can be used only in conjunction with the Swap Image behavior. There are no settings for it other than setting the event, which is onMouseOut by default.
Now add complexity to this image behavior. Put the cursor on the page and then select Insert > Image and choose gcanal1.jpg. This image is found in chapter_16/samples/images on the CD.
In the Name field next to the thumbnail in the Property inspector, type in canal.
Now you are going to use the Pisa image to change the canal image. This means that when you roll over the Pisa image, you expect the canal image to change.
Click the Pisa image. Double-click the existing Swap Image behavior. You can use this one instance of the behavior to control many images at once.
Choose the canal image. For Set Source To, click the Browse button and choose gcanal2.jpg from the chapter_16/samples/images folder.
Now both images in the list should have a source file.
Click OK to close the dialog box, and then Preview in Browser.
Roll over the Pisa image and both images should change. Mouse out and they both switch back.
Roll over the canal image and nothing happens. There is no behavior attached to it.
Now give this one more level of complexity. In Dreamweaver, click the canal image. In the Behaviors panel, click the plus (+) button and add a Swap Image behavior.
In the dialog box, select canal in the list and for source select gcanal2.jpg. Click OK to set the behavior.
Now you expect that rolling over the canal image will cause a swap to occur.
To view the final result, open swap_image.htm in the chapter_16 folder.
Preview in Browser.
Rolling over the Pisa image will still cause both images to swap. Rolling over the canal image will cause only the canal image to swap.
Borrowing function calls and moving functions to shared files are two really cool and sneaky techniques to learn with behaviors.
After you understand how functions and function calls work in Dreamweaver behaviors, you can extend your use of behaviors by "borrowing" their function calls to put in unusual places, essentially allowing you to attach behaviors to elements not normally allowed by the Dreamweaver interface. For instance, you can call behaviors from within media elements, like Flash or QuickTime movies. You can even call behaviors from within other behaviors.
It works like this:
Start by creating a temporary text link (something you'll delete when you're done). Somewhere in the document, type a word or two of text, and link it to # (a null link).
With the new text link selected, open the Behaviors panel and apply the behavior whose function call you want to borrow. (Maybe you want to call the Popup Message behavior from within a Flash Button, for instance. You would start by applying that behavior to your temporary text link.) Configure the behavior as desired. Test it out in a browser to make sure it does what you need it to do.
3. In Code view, find the code for your text link (see Figure 16.9). It will include the function call from the behavior, like this (function call is shown in bold):
<a href="#" onMouseOver="MM_popupMsg('Hello, world!')">click me</a>
Select and copy the function call code all the code between the double quotes, starting after onMouseOver= (just like the bold code shown here). Don't include the quotes themselves, though!
Delete the temporary text link. Be careful here, however! If you just select the link in Design view and delete, Dreamweaver will remove the function as well as the function call. So instead, go to Code view and delete the item from there. (After you've deleted the link, look through the document <head> to make sure the main function is still present.)
Any time you use a Dreamweaver behavior repeatedly across a site, you have an opportunity to streamline your code by moving the function code to a shared file. Dreamweaver will even help you. You must cut and paste the function code to a new JS file, and create a link between the two documents. You can then easily share that JS file between documents, and Dreamweaver will recognize its presence.
To move one or more function calls to a shared JS file:
Open an HTML file that contains one or more applied behaviors that you know you'll be re-using in other documents.
In Code view, scroll to the <head> section of the document and find the <script> tag containing your behaviors' functions.
Select everything between the opening and closing <script> tags (but not the tags themselves), and select Edit > Cut. (See Figure 16.11.)
When the new file opens, place the insertion point in a new paragraph after the opening comments and Edit > Paste. (See Figure 16.12.)
Save the file, with the .js extension, into your site. (After you've created this shared file, you can simply add other behavior functions to it later; you don't have to create a new JS file every time. Each site you work on can have its own shared JS file.)
Close the JS file, and return to your HTML document.
Your behaviors are now broken, until you link your HTML document with the new shared JS file. To link the shared file, do this:
Go to View > Head Content, so the Document window shows the little gray strip of icons that represent your document head.
Find and select the script icon. (This should select the now-empty <script> tag from which you removed the functions.)
In the Property inspector, find the Src field. Click its Browse button, and browse to the shared JS file. (See Figure 16.13).
After you've done this, save your document and Preview in Browser. If you did everything correctly, the behavior should still work.
The major benefit of creating JS files is that you can reuse them in other HTML documents. To reuse a shared behavior:
Open another document in which you want to use the same behavior. Don't apply the behavior yet!
Go to View > Head Content.
Open the Assets panel, and choose to view Scripts. Your shared JS file will appear there.
Drag the shared file from the Assets panel to the head content bar in your Document window. If you examine your code after having done this, you'll see that a link to the shared file has been added. (See Figure 16.14.)
Now you can apply any behavior that uses the functions in the shared JS file. Dreamweaver will know not to add the functions to your document, because they're already present in the shared file.
If you love the idea of sharing functions, but want some help with it, try Paul Boon's "Create and Hot Swap JS Files From Script Tags" extension, available from the Macromedia Exchange.
Behaviors go a long way to giving your web page advanced functionality and that professional flair. They enable you to go well beyond the limitations of basic HTML.
Other behaviors are available, and these are discussed throughout the book in chapters relevant to their specific functionality. You also can download and install any other functions in Dreamweaver that will go beyond the set included here. You can find these at the Macromedia Exchange for Dreamweaver. The Macromedia Exchange for Dreamweaver is discussed in Chapter 35.
In the next chapter, you will learn about even more behaviors and how they relate to using layers in your page design.