|
|
Extending Dreamweaver with its JavaScript API06/15/2001Welcome 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. The goal of this column is to help you get comfortable working with Dreamweaver's APIs before we get into creating more sophisticated solutions up the road. While this week's example is relatively simple, our work will become a little more complex in the next column, as we'll continue to explore the Dreamweaver JavaScript-APIs and build a tree-style menu widget along our way, which will let you create your site navigation with a simple click. But, let's finish our own custom command first. What you'll needIn 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. Additional resourcesIt's a good idea to download the following documentation to help you follow this article. The definitive reference for Dreamweaver's JavaScript API and configuration issues is Macromedia's Extending Dreamweaver 3, which is available in a few languages at the Support Center. The reference for the latest version, Dreamweaver 4, is currently only available in English, but is nevertheless better reading because it has links to all functions. Working with documentsAccessing 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 ModelDreamweaver 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. Those new to the world of the DOM can refer to a previous Essential JavaScript column for an introduction to the basics of the DOM. If you're already familiar with Netscape's or the W3C's DOM, you won't find a lot of surprises. The DOM Level 1 implementation is not complete, but it is good enough to let you access and manipulate every element. Unfortunately, the handy
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 Accessing a selectionRetrieving 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:
The 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.
|
|
|
|
|
||||||||