User Guide
A simple toolbar command file 195
Writing the JavaScript code
When the user interacts with the text box, it causes Dreamweaver to invoke the EditTitle.htm
command file in the Toolbars/MM folder. This file contains three JavaScript functions that
operate on the Title text box. These functions are
canAcceptCommand(),
receiveArguments(),
and getCurrentValue().
canAcceptCommand(): enable the toolbar item
The
canAcceptCommand() function consists of one line of code that checks to see if there is a
current DOM and if the document is parsed as HTML. The function returns the result of those
tests. If the conditions are
true, Dreamweaver enables text box item on the toolbar. If the
function returns the value
false, Dreamweaver disables the item.
function canAcceptCommand()
{
return (dw.getDocumentDOM() != null && dw.getDocumentDOM().getParseMode() ==
'html');
}
receiveArguments(): set the title
Dreamweaver invokes the
receiveArguments() function, shown in the following example,
when the user enters a value in the Document Title text box and presses the Enter key or moves
the focus away from the control:
function receiveArguments(newTitle)
{
var dom = dw.getDocumentDOM();
if (dom)
dom.setTitle(newTitle);
}
Dreamweaver passes newTitle, which is the value that the user enters, to the
receiveArguments() function. The receiveArguments() function first checks to see if a
current DOM exists. If it does, the
receiveArguments() function sets the new document title
by passing
newTitle to the dom.setTitle() function.
getCurrentValue(): get the title
Whenever an update cycle occurs, as determined by the default update frequency of the
onEdit
event handler, Dreamweaver calls the getCurrentValue() function to determine what value to
display for the control. The default update frequency of the
onEdit handler determines the
update frequency because the Title text edit control has no update attribute.
For the Document Title text box, the following
getCurrentValue() function calls the JavaScript
API function
dom.getTitle() to obtain and return the current title:
function getCurrentValue()
{
var title = "";
var dom = dw.getDocumentDOM();
if (dom)
title = dom.getTitle();
return title;
}