Commands > A simple command example |
The following command converts the selected text to all lowercase characters. The command is very simple; it does not display a dialog box, so the commandButtons() function is not defined.
<HTML>
<HEAD>
<TITLE>Make Lower Case</TITLE>
<SCRIPT LANGUAGE="javascript">
function canAcceptCommand(){
// Get the DOM of the current document
var theDOM = dw.getDocumentDOM();
// Get the offsets of the selection
var theSel = theDOM.getSelection();
// Get the selected node
var theSelNode = theDOM.getSelectedNode();
// Get the children of the selected node
var theChildren = theSelNode.childNodes;
// If the selection is not an insertion point, and
// either the selection or its first child is a
// text node, return true.
return (theSel[0] != theSel[1] && (theSelNode.nodeType == ¬
Node.TEXT_NODE || theChildren[0].nodeType == Node.TEXT_NODE));
}
function changeToLowerCase() {
// Get the DOM again
var theDOM = dw.getDocumentDOM();
// Get the offsets of the selection
var theSel = theDOM.getSelection();
// Get the outerHTML of the HTML tag (the
// entire contents of the document)
var theDocEl = theDOM.documentElement;
var theWholeDoc = theDocEl.outerHTML;
// Extract the selection
var selText = theWholeDoc.substring(theSel[0],theSel[1]);
// Re-insert the modified selection into the document
theDocEl.outerHTML = theWholeDoc.substring(0,theSel[0]) + ¬

selText.toLowerCase() + theWholeDoc.substring(theSel[1]);
// Set the selection back to where it was when you
// started
theDOM.setSelection(theSel[0],theSel[1]);
}
</SCRIPT>
</HEAD>
<BODY onLoad="changeToLowerCase()">

<!-- The function that does all the work in this command is ¬

called from the onLoad handler on the BODY tag. There is no ¬

form in the BODY, so no dialog box appears. -->
</BODY>
</HTML>