|[ LiB ]|
An alternative approach to putting nested content on pages is to change the contents of a single layer instead of hiding and showing multiple layers . In Dreamweaver, you do this with the Set Text of Layer behavior.
As coded by Dreamweaver, the Set Text of Layer behavior works in IE 4+, Netscape 4, and Netscape 6/Mozilla. It does not work in Opera 5 and 6, but it does work in Opera 7.
The Set Layer Text behavior is tucked away in the Set Text submenu of the Behaviors panel's Actions pop-up menu. Choosing it brings up a dialog box in which you can choose any of your document's layers and enter any text you want to appear in that layer (see Figure 14.11). Whatever you enter here will replace the existing layer contents, regardless of the contents. You can even change the contents of the layer containing the object the behavior is attached to. If you leave the dialog box's input area empty, the contents of the specified layer will be deleted.
What can you do to really take advantage of this behavior?
Despite its name , Set Text of Layer is not limited to text effects. You can use it to put almost any content into a layer, by entering HTML code rather than straight text into the input field of the dialog box. Code such as this will display in a formatted text message in the specified layer:
<h1>Welcome!</h1> <p>Are you ready for the <b>big</b> moment?</p>
Tables and images, links, and forms can all be written into layers using this behavior. A more accurate description for the behavior might be "Set HTML Content of Layer."
Here are a few tips to consider when using Set Text of Layer.
Whatever text or HTML you enter in the dialog box will be inserted into the behavior's function call, like this:
If you have set your Code Rewriting Preferences to URL-encode special characters and attribute values, any HTML source code that you enter will be inserted into your page code looking like this:
onClick="MM_setTextOfLayer('help','','%3Cimg src=%22duck.gif%22 width=%2250%22 height=%2250%22%3E')"
Although this code will work perfectly fine in a browser, it isn't too readable if you later want to hand-edit your HTML. You can avoid this mess by going to Edit > Preferences > Code Rewriting (Mac OS X: Dreamweaver > Preferences > Code Rewriting) and disabling both Special Characters optionsthe Encode Special Characters in URLs and Encode <, >, &, and " in Attribute Values Using & options. With these options deselected, Dreamweaver will escape all quotes with \ and leave all other characters alone. If you do this, however, you must include only single quotes in your HTML code. This code, for instance, will break the behavior:
For a full discussion of functions and function calls in Dreamweaver behaviors, see the section "Working with Behaviors in Dreamweaver" in Chapter 13. For more on the Code Rewriting preferences, see the section "Using Dreamweaver as a Text Editor" in Chapter 27, "Writing Code in Dreamweaver."
<img src="duck.gif" width="50" height="50">
But this code will work fine:
<img src='duck.gif' width='50' height='50'>
It will be inserted into your page code as the following function call:
onClick="MM_setTextOfLayer('violin','','<img src=\'duck.gif\' width=\'50\' height=\'50\'>')"
If you want to insert HTML formatting using Set Text of Layer, but you don't want to type all that code yourself, work smart with Dreamweaver.
Create the desired display in Design view, either in the same file you're working on or in a temporary file. Then go to Code view and copy the code from there. Open the Set Text of Layer dialog box, click in the input area, and paste. Remember, though, that Dreamweaver always encloses tag attributes in double quotes. So, if you have disabled the Special Characters options as described earlier, you'll have to replace all double quotes with single quotes, either by hand or by using the Find and Replace command, before the behavior will work.
Some browsers won't display embedded media properly when the <embed> code appears as part of this command. Media objects and layers often don't mix well.
This exercise builds an interactive illustration that puts different data in an information layer depending on what part of the illustration the mouse rolls over. All the files for the exercise can be found in the chapter_14/violin folder on the book's website.
<h1>Scroll</h1> <p>Scrolls are curly and brown, and have wooden pegs sticking _out of both sides.</p>
<area onMouseOver="MM_setTextOfLayer('violin','', '%3Ch1%3EScroll%3C/h1%3E%0D%3Cp%3E Scrolls are curly and brown, and have wooden pegs sticking out of both sides.%3C/p%3E')" shape="rect" coords="65,3,145,89" href="#">
<area onMouseOver="MM_setTextOfLayer('violin','',' <h1>Scroll</h1><p> Scrolls are curly and brown, and have wooden pegs sticking out of both sides.</p>')" shape="rect" coords="65,3,145,89" href="#">
Neck Violin necks are long and skinny, and since they're made of wood they're not much good for swallowing.
Let Dreamweaver's color coding work for you. If your pasted code contains incorrect quote marks, the improperly terminated string literals will turn black. If the quote marks are correct, the entire function call (everything after onMouseOver=) will be blue.
A lovely, efficient way to put nicely formatted text into a layer without having to enter and re-enter the HTML formatting for each new set of text is to assign a custom CSS class to the layer itself. It works this way (see Figure 14.16):
After this is done, whenever you Set Text of Layer, you need to enter only the text itself. All formatting will be supplied.
Repeatedly setting the text of a single layer can create a similar effect to starting with a stack of hidden layers and showing them one at a time. Why choose one method over another?
Loading and preloading. If all of the document's content is present when the page initially loads (as it is when using Show-Hide Layers), it will all display immediately when called on. Depending on your project, you might decide that this is a good thing or a bad thing. If you know ahead of time that users are probably going to access all the content, you'll want to download it as soon as possibleso use Show-Hide Layers. If you think users will probably want to access only one or two layers' worth of contents, you might not want to make them wait for the other layers' content to downloadso use Set Text of Layer.
Simple text changes. If your content all consists of similarly formatted text, it will probably be more efficient to create and edit content using Set Text of Layer. You can use a CSS style applied to the layer to control formatting, and you need enter only unformatted text in the behavior's dialog box.
Different content, identical layers. If your content will all appear in the same position on the page, it's easier to format, resize, and position one layer than several. Use Set Text of Layer.
Browser compatibility. Setting layer text as it is scripted in the Dreamweaver behavior is supported by all current versions of Netscape and IE, but not by versions of Opera earlier than Opera 7. If this extra browser support is important to you, stick with Show-Hide Layers.
|[ LiB ]|