- Scope of Variables and Functions
- Handling Events
- Sharing Data
- Delays and Timeouts
Objects, elements, and properties
- When GoLive loads a markup document, it generates objects to represent the markup tree of a document. Collectively, the objects that represent portions of the markup document are known as markup objects. Markup objects can represent HTML markup elements (as defined by a tag and its attributes), and also comments, text blocks, and other types of markup such as entities or CDATA sections.
- You can retrieve most component objects by name from global properties, such as the menus and dialogs collections.
- Objects that represent HTML page content are available from the markup tree; you get the root object from the document Object (page 138) for the page (document.documentElement), and that object’s properties and methods allow you to navigate the tree.
- GoLive passes relevant objects as event-object property values to event-handling functions.
For information on retrieving a particular object, see that object’s description in the GoLive CS2 SDK Programmer’s Reference.
Accessing attribute values
// correct test if (typeof (myProperty) != undefined) // do something
Do not use the following test. This tests the property’s value, rather than its state, and results in a run-time error if the property does not exist:
// incorrect test if (myProperty != undefined) // if myProperty does not exist, an error occurs
if (currElt.tagName.toLowerCase()) == (tagToGet.toLowerCase())
For element Objects (page 156), attributes are also represented by objects, which are themselves nodes in the markup tree. Use an element object’s getAttributeNode and setAttributeNode functions (page 157) to access the attribute object, rather than accessing the attribute directly by name, as a property of the element object. By using these methods, you avoid potential problems with referencing names that contain special characters, such as hyphens.
Naming objects and attributes
<jsxmenubar> // opens definition of all menus <jsxmenu name="Hello" title="Hello, GoLive!"> //Hello menu <jsxitem name="This" title="Do Something"> // menu item <jsxitem name="That" title="Do Something Else" > menu item </jsxmenu> // closes definition of Hello menu </jsxmenubar> // closes definition of all menus
var myMenu = menubar["ADBEHello"];
The SDK makes commonly used objects available as the elements of array-like structures that all extensions can access. GoLive updates the contents of these structures dynamically as these objects are created and deleted.
The SDK implements many of these structures as collection Objects (page 72). This is like an array that provides access to its elements by name or index; however, collections are not actually arrays; not every collection provides numeric access to its elements, as an array object does.
Each of these global properties contains a collection object that GoLive updates dynamically:
Using the global object arrays
These examples use the menus array to illustrate how you retrieve objects from global arrays. These arraya provide access to all of the menus and menu items added to GoLive by extensions. Most of the arrays work the same way; exceptions are noted in the Programmer’s Reference, Part 2 of this book.
<jsxmenu name="sample" title="Sample" ...> <jsxitem name="item1" title="MyItem" ...> </jsxmenu>
The following retrieves the Sample menu from the menuCollection object in the menubar global variable, and stores the retrieved menu object in the sampleMenu variable:
var sampleMenu = menubar["sample"]
In this case, “Sample” is the title of the menu, as displayed to the user, while “sample” is the name of the menu object, which you use to access it programmatically.
The following retrieves the menu item by name from the collection in the items property the sample menu:
Alternatively, you can retrieve the menu item directly, using its name as a property name of the sample menu:
Many collections can be accessed by numeric index as well as by name. For example, if item1 is the first menu item:
menubar["sample"].items // 0-based index of first item
This is only reliable for the items, not for menus; because other extensions can also add menus, you cannot rely on the order. Some collections, like the controls collection, do not support numeric access at all. Most of the time, an object’s unique name property provides the most reliable way to retrieve it.
To ascertain an object’s identity, you can compare the value of its name property to a known string, or you can compare object references directly. For example, you can test the name of a menu item in any of the following ways:
if (item.name == "item1") // compare object name to known string value if (item == menubar["sample"].items["item1"]) // compare objects if (item == sample.item1) // another object comparison example
Updating references to objects
GoLive generates objects to represent the markup tree of a document when it loads that document. It regenerates these objects if the document changes; this is know as reparsing the document. If you save a reference to an object that GoLive generated as the result of interpreting a markup tag, you must update that reference any time the document containing the tag changes. For details, see the full version of the Programmer’s Guide on the product CD.