Workshop #1: Creating a Simple BehaviorIt might take a bit of thought to wrap your brain around this, but you're about to write a JavaScript that instructs Dreamweaver to write some JavaScript in the user's document. Why would you want to do this? You (as an author) write behaviors for scripting tasks that you (as a user ) need repeatedly, but don't want to have to manually script over and over again. You also might want to write behaviors to share with your less scripting-savvy colleagues so that they can put them in their documents with the click of a + button. Are there any bits of JavaScript you find yourself always entering by hand, either because Dreamweaver doesn't provide them or because you don't like the way the Dreamweaver versions are scripted? Do you always end up using Dreamweaver behaviors and then tweaking the code by hand to make it just the way you want it? If so, you should start making your own behaviors. Sample Behavior: Adding "Back" and "Forward" Controls to a DocumentWith a short chunk of JavaScript, web page creators can put text links or buttons on their pages that will navigate through the visitor's browser history, mimicking the functionality of the browser's Back and Forward buttons . Why would someone want to do this? It can be a nice added element in a web site's navigation controls, and it doesn't rely on the visitor using the browser's built-in controls. In this workshop, you create a behavior that causes Dreamweaver to add a Back or Forward link to a piece of text or an image. The behavior includes a dialog box that asks which way the link should goback or forward. Nice and simple, eh? Creating the Back/Forward BehaviorBecause the API for behaviors contains so many required and optional elements, creating even a fairly simple behavior like this one involves many tasks: creating and testing the JavaScript code to insert, building the basic behavior file, collecting and processing user input, revising and testing. This workshop takes you through each of those tasks. Complex tasks are broken down into steps for you. Task 1: Create the JavaScript code insert A behavior is only as good as the JavaScript it inserts . Our first task, therefore, is to create the properly formatted function and function call that you want the behavior to insert. We'll do this in a test file.
note If you're an expert JavaScript writer and are used to having lots of choices in how you structure your scripts, you might be asking yourself a few questions here. Why did you create a generic function and pass it different parameters each time, instead of creating two different functions? And why did you link to # and use an event handler instead of linking directly to "javascript: goThere(1);" ? Why did you need to use a function at all for such a short line of code? You could have written <a href="javascript: window.history(-1); ">Back</a> . You did it this way because Dreamweaver applies these restrictions when you turn this script into a behavior. All behaviors create generic functions and usercustomizable function calls. Task 2: Insert the code into a behavior file framework Okay, the script works. It's structured as a function and function call. It's time to turn this puppy into a Dreamweaver behavior.
Task 3: Create a form to collect user input Of course, if you always want the behavior to send the browser back to the previously visited page, you don't need a form. But you want your behavior to be a little more versatile than that, allowing the user to specify for each instance of the behavior whether it should send the browser window back or forward. This requires adding a form. Because the user needs to choose only between back and forward, the form needs to contain only a few text labels and a pair of radio buttons. The completed form might look something like the one shown in Figure 3.14. Figure 3.14. The desired layout for the Go Back/Forward behavior's dialog box.
To create the form, open the Back or Forward.htm behavior file in your text editor and change its <body> code to look like this: <body> <form name="myForm"> <table> <tr> <td nowrap colspan="2">Direction to go:</td> </tr> <tr valign="baseline"> <td nowrap align="left"> <input type="radio" name="direction" value="-1" checked>Back </td> <td nowrap align="left"> <input type="radio" name="direction" value="1"> Forward </td> </tr> </table> </form> </body> Task 4: Rewrite the applyBehavior() function so that it builds the function call from user input The applyBehavior() function, like the objectTag() function in the last chapter, must return a string of code that will be inserted in the user's document. Any user input collected from the behavior's dialog box must be built into this code string by concatenation ( putting variables and text strings together, like you did to create object code in the previous chapter). For the Go Back/Forward behavior, the function call must include either 1 or - 1 , depending on whether the user selects the top or bottom radio button.
note If you open either fwd_test.htm or back_test.htm in Dreamweaver and select one of the back or forward text links, the Behaviors panel will tell you that this link has the Go Back/Forward behavior applied to it. Even though you coded these files by hand, you used the same function name and structure (the goThere() function with one passed parameter) in the hand-coded version as you later used in creating the behavior. This causes Dreamweaver to interpret the function as an instance of the behavior. If you've come this far, you've created a completely functional behavior. Congratulations! Listing 3.2 shows the final code for the Go Back/Forward behavior. Listing 3.2 Code for the Finished Go Back/Forward Behavior, with Comments Added for Reference<html> <head> <! title becomes actions menu item name > <title>Go Back/Forward</title> <script language="JavaScript"> //this is the function the behavior will insert into the user's document function goThere(page) { window.history.go(page); } //this returns the name of the function to be inserted, minus its only parentheses function behaviorFunction() { return "goThere"; } //this returns the function call, which will be inserted as part of the user's selected object function applyBehavior() { var whichWay; //this if-statement sets the variable based on which radio button has been selected if (document.myForm.direction[1].checked) { whichWay = 1; } else { whichWay = -1; } //this statement constructs the function call return "goThere("+whichWay+")"; } </script> </head> <body> <form name="myForm"> <table> <tr> <td nowrap colspan="2">Direction to go:</td> </tr> <tr valign="baseline"> <td nowrap align="left"> <input type="radio" name="direction" value="-1" checked>Back </td> <td nowrap align="left"> <input type="radio" name="direction" value="1"> Forward </td> </tr> </table> </form> </body> </html> |