|MySQL Conference and Expo April 14-17, 2008, Santa Clara, CA|
Welcome back to the second part of our Dreamweaver series. We talked about configuration issues in part one, and today we'll move forward by dissecting the first custom command to determine the document's number of words and characters.
What you'll need
In order to follow along with this article, you'll need Dreamweaver or Dreamweaver UltraDev version 3 or later. You can download a free 30-day evaluation copy of Dreamweaver 4 at Macromedia.
Working with documents
Accessing a window or document is quite easy when you're hosted inside a browser. You get a reference to a window via the frames array from properties such as "opener" or a variable. Dreamweaver must naturally take a different approach, so, in order to find the active document, you'd write:
In contrast to the method's name, it returns a DOM document rather than a window object. The
This method expects one argument, which determines the document to access --
Accessing the current document works just like it does in a browser. When your code is running as a command, the current document is the dialogue shown.
The Document Object Model
Dreamweaver 3 introduced a lot of new features. One of them is a new and enhanced Document Object Model, which is a mixture of Netscape's homegrown DOM and a subset of DOM Level 1.
So, after finding the needed document (see above), you can access its document structure through DOM's
The next step will tell you why I accessed
Retrieving a document's selection is not part of the DOM specs, so we need to use proprietary methods, which are all extensions of the document object. To access the current selection you write:
To make the command more flexible, we check the extracted text's length. If it's zero, we assume the user wants to apply the command to the whole document instead to a selection.
Dreamweaver's Selection API also allows you to select or substring, an element, or a table programmatically.