[ LiB ] |
The fun of using Dreamweaver behaviors is what you can do with specific behaviors to snazz up your page or make it that much more functional. The hard part is getting used to the quirks each behavior brings with it and knowing how to use each one well.
If you want a behavior to be triggered by rolling over or clicking an image, you have a choice: Make the image a null link and attach the behavior to the a tag, or attach the behavior directly to the img tag. The first gets coded like this:
<a href="#" onMouseOver=" [behavior code] "> <img src="dog.jpg"> </a>
The second gets coded like this:
<img src="dog.jpg" onMouseOver=" [behavior code] ">
There are two advantages to creating a link and using that to trigger the behavior. First, if the image is a link, the cursor will change when the user rolls over it, which means that the user is more likely to recognize this as something he can interact with and click on. Second, Netscape 4 doesn't support attaching behaviors to the img tag, so the behavior might not work unless you attach it to a surrounding link.
Because Netscape 4 doesn't support adding actions to images, if you have your Behaviors panel set to display events for 4.0 and Later Browsers, or 3.0 and Later Browsers, Dreamweaver won't let you assign an action directly to an image. If you select an image and choose a behavior, the program looks for a link surrounding the image. If there isn't one, it adds a null link. Then it attaches the behavior to that link.
To make the whole process more user-friendly, however, Dreamweaver still treats the behavior as if it were associated with the image. If you select the image, the Behaviors panel will display the behavior, but with an <A> indicator in the event handler area (see Figure 13.3). (Previous versions of Dreamweaver displayed the event handler in parentheses.)
This means that the actual code for the behavior has been put in the a tag. You can inspect and edit the behavior, even though, strictly speaking, the tag that you have selected ( img ) has no behaviors attached to it.
But what if you want your behavior attached to the image itself and not the link? Select the image, find the behaviors with the <A> event handlers, and use the drop-down menu to assign a non- <A> event handler (see Figure 13.4). This moves the code from the link to the image. Note that to show event handlers for the image itself, you need to show events for one of the later browser versionsI.E. 4+ or Netscape 6.
With the Check Browser behavior (see Figure 13.5), 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.
This might be required only if you have a lot of DHTML, with animated layers and other elements that might be prone to cross-browser issues. This behavior uses JavaScript to determine the browser name and version and then automatically links to the determined path .
To use this behavior, you are deciding a few things. The behavior decides among IE, Netscape, and others. For testing this behavior, you need at least three browsers installed. Also, you need to make pages that let you know that it is working. For instance, you need to make a page that says "This is Netscape" or "This is IE" and "This is the other page."
There are also a couple of ways to set this up. Because the behavior is going to happen as the page is loading, you can set up an empty page that contains only the behavior. It will then direct you to the correct page. Or, for browsers that don't support JavaScript or those that have disabled it, put the behavior on the initial page. This page should have content for those users. Others will be directed accordingly .
You have three choices for redirection:
Stay on This Page
URL
Alt URL
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?". Those with version 3 browsers can be sent somewhere else.
For this example, you will set up the behavior so that the other browser will stay on the same page and so that 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 such as Netscape 3 and 6, and IE 4 or above.
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 that user 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, be sure to test all your functionality as you go.
Call JavaScript is a behavior that inserts JavaScript code that you write yourself. Instead of inserting some prewritten JavaScript action, however, the behavior adds whatever JavaScript statement(s) you specifically tell it to add. To use this behavior, open a document; select the element that you want to trigger the behavior; and, from the Behavior panel's Actions list, choose Call JavaScript. In the dialog box that appears, type in whatever JavaScript statement or statements you want to execute (see Figure 13.6). Separate multiple statements with semicolons.
This is a handy behavior for quickly adding bits of JavaScript code to a document, without having to worry about coding event handlers, functions, and function calls, and even without having to leave Design view. Of course, you can't really use the behavior unless you know enough JavaScript syntax to know what code you want to add, so it's not for everybody. But if you want to dip your toes in the JavaScript waters just a bit without having to master event handlers and such, Call JavaScript might be just what you need. Table 13.1 shows some handy one- and two-line code statements that can be entered into the Call JavaScript dialog box.
JavaScript Code | What It Does | Special Instructions |
---|---|---|
history.back() history.forward() | Sends the browser back or forward as if the user had clicked the browser's Back or Forward buttons . | |
window.close() | Closes the current browser window. This is useful when multiple browser windows are open. | |
window.resizeTo ( width, height ) | Resizes the current browser window to the specified dimensions. This is handy when the page first loads. Select the body tag and attach it using the onLoad event. | Substitute numbers for width and height. |
document. formname. textfieldname. focus() | Puts the insertion point in a specified text field, in a specified form. This is useful when a page contains a form because the user can immediately start typing in the first text field as soon as the page loads. Select the body tag and attach it using the onLoad event. | Substitute the name of your form and the name of the text field you want to put the insertion point in. |
Note for hand-coders: Because Dreamweaver behaviors must always insert their JavaScript as a noncustomizable function and a function call with parameters, Call JavaScript can lead to more cumbersome coding than if you just dipped into Code view and added the code there. For more on Dreamweaver's JavaScript tools, including Call JavaScript, see the section "Writing JavaScript in Dreamweaver" in Chapter 27, "Writing Code in Dreamweaver." |
The Open Browser Window behavior (see Figure 13.7) opens a new browser window. You can specify what page appears in that window, and you get to determine exactly what the window looks like: how big it is, whether it's resizable, and even what bits of window "chrome" (button bar, status bar, scrollbars, and so on) it displays. This behavior is great when you want a message to pop up, but you want it to look nicer than the generic browser alert window called up by the Show Pop-Up Message behavior; or when you want to open a page in a new window, but you need more control than simple _blank targeting can offer.
You don't need to use JavaScript just to display a web page in a new browser window. You can accomplish that with a standard hyperlink by setting its target attribute to _blank . With this targeting method, however, you can't control the size or appearance of the browser window. For that, you need the behavior. |
To use the Open Browser Window behavior, start by creating or getting the URL for the page that you want to appear in the new window. Then open the page that you want to trigger the behavior from; select a page element; and, from the Behaviors panel's Actions list, choose Open Browser window.
In the dialog box that appears, enter the specifics for the new window.
If you want to make sure your visitors don't mistake this new window for the main browser window behind it, and if you want to save screen space, don't turn on all the chrome elements. In fact, most designers leave most of them off (see Figure 13.8). But it's a good idea to leave the resize control and scrollbars on, in case your users need them.
It's also a good idea to give the new window a name (a simple, one-word name will do). Why? If your main page has several links to new windows, or if the user clicks on your one link to the new window several times, you probably don't want this to open multiple new windows. It can get awfully crowded in there! But if the new window has a name, the browser knows not to open more than one of it. If you call your window mini , for instance, and the user opens it, loses it behind something else, and then tries to open it again, the browser will see that mini already exists and will bring it to the front instead of spawning another window.
It's not the most common situation in the world, but if you want to include a link in the main window that changes the page in the new window, you can use the new window's name as the link's target, like this: <a href="otherpage.html" target="mini"> . |
When deciding what size to make your window, be aware that not all browsers or platforms size their windows the same. (Table 13.2 shows some sample size variances.) You don't want content failing to display properly because the window isn't big enough.
Platform | Browser | Viewable Window Area |
---|---|---|
Windows | IE | 400 x 400 (no chrome) 385 x 420 (all chrome) |
Windows | Netscape/Mozilla | 400 x 400 (no chrome) 400 x 400 (all chrome) |
Mac | Safari | 400 x 400 (no chrome) 400 x 350 (all chrome) |
Mac | IE | 415 x 415 (no chrome) 415 x 415 (all chrome) |
Mac | Netscape/Mozilla | 400 x 400 (no chrome) 400 x 400 (all chrome) |
By default, the new browser window that opens appears tucked up into the upper-left corner of the user's screen. Unfortunately, the behavior doesn't include an option for changing that. But you can fix that if you don't mind just a small amount of hand-coding.
When the behavior is in place in your document, go to Code view and look in the head section for this function:
function MM_openBrWindow(theURL,winName,features) { //v2.0 window.open(theURL,winName,features); }
Determine, in pixels, how far away from the upper-left corner you want the window to appear. You need to supply it as x,y coordinates (x = horizontal, y = vertical). Then add the following to code to the function:
function MM_openBrWindow(theURL,winName,features) { //v2.0 newWin = window.open(theURL,winName,features); newWin.moveTo(x,y); }
For x and y, substitute the horizontal and vertical coordinates you determined earlier.
Or, if you're up to a slightly longer bit of typing, you can get the window to center itself on the user's computer screen, no matter what size it is. Instead of the previously added code, change the function like this:
function MM_openBrWindow(theURL,winName,features) { //v2.0 newWin = window.open(theURL,winName,features); newWin.opener.name="main"; newWin.moveTo((screen.width/2)-200,(screen.height/2)-200); }
This one's trickier because you have to watch out for all those parentheses in the fourth line. But that's all there is to it! Note, however, that the code written here centers a 200 x 200 browser window. Change the occurrences of 200 in the fourth line to the width and height you have specified for your new window.
Be aware, though, that after you've made these tweaks, this isn't a plain out-of-the-box Dreamweaver behavior anymore. If you edit it through the Behaviors panel, your custom code can be overwritten.
All of your computer-savvy users will know how to close the new little window you've opened by clicking its Close box. But it's a nice idea, for those who don't know, to give them an easy-to-see little Close button in the window itself (see Figure 13.9). It's just a matter of opening the page that will display in the little window, adding a text or graphic link to its layout, and attaching a bit of JavaScript to it that tells the browser to close the window. Dreamweaver has no behavior for this, but it's so easy that there's not much need for one. Just select your Close text or graphic and, in the Property Inspector's Link field, type this:
javascript:window.close()
Figure 13.10 shows how the whole procedure looks in Dreamweaver.
This short bit of JavaScript code could also be added using the Call JavaScript behavior. The resulting code that gets added to your page is more compact if you add the code through the Link field, however. |
One possible use for a small new browser window is to contain links to exciting places in the main website (see Figure 13.11). Some designers refer to this as a controller window. It works this way: The links in the small window are regular hyperlinks that are targeted to the main window, so clicking them opens the linked page in that window. The trick is, the main window needs to have a name to use as the target value.
Controller windows can also be used to open a third browser window and control the pages that appear in that window. In these cases, the main window sits behind the two new windows. |
Naming the main window is usually done by the same script that opens the new windowthe Open Browser Window behavior, in other words. By default, the behavior doesn't do this, but, guess what? A little code tweaking can easily meet this challenge.
When the behavior is in place in your document, go to Code view and look in the head section for this function:
function MM_openBrWindow(theURL,winName,features) { //v2.0 window.open(theURL,winName,features); }
Now change it to look like this:
function MM_openBrWindow(theURL,winName,features) { //v2.0 newWin = window.open(theURL,winName,features); newWin.opener.name="main"; }
You can substitute any one-word name you like for main . Just remember what you enter here because you'll need it for targeting your links later.
In this exercise, you'll create a miniwindow that pops up from the Kenswear home page, directing visitors to highlighted pages within the site. If you haven't done so yet, download the chapter_13 folder from the book's website at www.peachpit.com and create a site with this as the root folder. The files for this exercise are in the ken folder.
function MM_openBrWindow(theURL,winName,features) { //v2.0 newWin = window.open(theURL,winName,features); newWin.moveTo(200,200); newWin.opener.name = "main"; }
window.focus()
Swap Image (see Figure 13.15) is undoubtedly the most popular of the Dreamweaver behaviors because it's nice and stable and it creates rollovers. Everybody loves rollovers! In the world of web design, they've become an expected way of making buttons announce to the world that they're buttons and can be clicked on. But Swap Image can also be used to create more complex effects, such as disjoint rollovers.
The Swap Image behavior changes the value of the src property of an img tag so that another image is displayed in the original image's place. No rule says that the image being swapped has to be the image that the behavior is attached to. When the behavior is triggered, it can swap any image in the current document. When an image swaps because the user clicked on or rolled over something other than the image itself, it's referred to as a disjoint , or complex , rollover.
Only a few rules are involved in working in Swap Image:
The Swap Image behavior is easier to understand if you remember that the img tag itself is just a placeholder in the document, where the browser is reserving space and expecting to display an image. The src attribute determines which image will fit into the space. The width and height attributes determine how much space to reserve. The Swap Image behavior swaps only the src , not the width or height . That's why the swapped image must display at the same size as the original. On another note, that's also how Dreamweaver's Placeholder Image object works, by creating an img tag with no src . |
To build a disjoint rollover, make sure you have a named image in your document and another image file somewhere in your site ready to swap for it. Then do this:
When you're done choosing images to swap, make sure the Preload Images and Restore on MouseOut options are set the way you want them, and click OK.
Not only can you swap any image with the Swap Image behavior, but you can also swap more than one image at a time with the same behavior. For instance, you might have a navigation bar button selected. You can assign one Swap Image behavior that will create a standard rollover and swap some other image on the page at the same time.
To perform multiple image swaps at once, go through the steps outlined in the previous section, but before closing the dialog box, repeat steps 3 and 4 to swap another image. Do this as many times as you want. Each image that will be swapped shows up in the dialog box's image list with an asterisk by its name, so you can spot them easily as you're scrolling through the list.
You're also not limited to swapping images on rollover. Maybe you want the image to change when it's clicked on instead of when it's rolled over, or in addition to the rollover swap. You can swap images for onMouseOver , onMouseDown , onMouseUp , onClick , or onMouseOut to create some interesting interactive image effects! (Remember to check that your target browsers support the events you use, though.)
In this exercise, you'll use some fancy image-swapping to create a clickable slide show for an art gallery. If you haven't done so already, download the chapter_13 folder from the book's website to your hard drive and define a site with this as the root folder. The files for this exercise are in the chapter_13/gallery folder.
The Show Pop-Up Menu behavior (see Figure 13.19) lets you create a submenu of links that will appear when the user clicks on a link on your page. This behavior is modeled after the behavior of the same name in Fireworks MX 2004, although, unlike its Fireworks cousin, it creates only text-based menus, where Fireworks can also generate image-based menus. If you open a document in Dreamweaver that contains a Fireworks-generated pop-up menu, Dreamweaver recognizes the code as a pop-up menu.
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:
In this exercise, you 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, download the chapter_13 folder from the book's website to your hard drive. Define a site called Chapter 13, with this folder as the local root folder.
Experienced scripters know that any code that will be used repeatedly should be accessed from a single, shared resource for greatest efficiency. This makes the code easier to read and edit, and it also trims download time and browser overheadespecially if the code is extensive . When dealing with JavaScript in HTML documents, this often means moving the functions to a linked JS file that can be shared between HTML documents.
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 even helps 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, follow these steps:
Your behaviors are now broken until you link your HTML document with the new shared JS file. To link the shared file, do this:
The major benefit of creating JS files is that you can reuse them in other HTML documents. To reuse a shared behavior, follow these steps:
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. |
When 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 enabling you to attach behaviors to elements not normally allowed by the Dreamweaver interface. For instance, you can call behaviors from within media elements, such as Flash or QuickTime movies. You can even call behaviors from within other behaviors.
It works like this:
<a href="#" onMouseOver= "MM_popupMsg('Hello, world!')" >click me</a>
See Chapter 14, "Controlling Layers with JavaScript," for examples of using this technique to call behaviors from other behaviors. See Chapter 16, "Building Web Pages with Flash," for examples of using the technique with media elements. |
[ LiB ] |