Adding the JavaScript functions
In this example, the JavaScript functions define the behavior and insert code for the Strikethrough object. You must place all the API functions in the HEAD
section of the file. The existing object files, such as Configuration/Objects/Text/Em.htm, follow a similar pattern of functions and comments.
The first function the object definition file uses is isDOMRequired()
, which tells whether the Design view needs to be synchronized to the existing Code view before execution continues. However, because the Superscript object might be used with many other objects in the Code view, it does not require a forced synchronization.
To add the isDOMRequired() function:
- In the
HEAD
section of the Strikethrough.htm file, between the opening and closingscript
tags, add the following function:<script language="javascript"> function isDOMRequired() { // Return false, indicating that this object is available in Code view. return false;}</script>
- Save the file.
Next, decide whether to use objectTag()
or insertObject()
for the next function. The Strikethrough object simply wraps the s
tag around the selected text, so it doesn't meet the criteria for using the insertObject()
function (see insertObject()).
Within the objectTag()
function, use dw.getFocus()
to determine whether the Code view is the current view. If the Code view has input focus, the function should wrap the appropriate (uppercase or lowercase) tag around the selected text. If the Design view has input focus, the function can use dom.applyCharacterMarkup()
to assign the formatting to the selected text. Remember that this function works only for supported tags (see dom.applyCharacterMarkup()
in the Adobe Dreamweaver API Reference). For other tags or operations, you may need to use other API functions. After Adobe Dreamweaver applies the formatting, it should return the insertion point (cursor) to the document without any messages or prompting. The following procedure shows how the objectTag()
function now reads.
To add the objectTag() function:
- In the
HEAD
section of the Strikethrough.htm file, after theisDOMRequired()
function, add the following function:function objectTag() { // Determine if the user is in Code view. var dom = dw.getDocumentDOM(); if (dw.getFocus() == 'textView' || dw.getFocus(true) == 'html'){ var upCaseTag = (dw.getPreferenceString("Source Format", "Tags Upper Case", "") == 'TRUE'); // Manually wrap tags around selection. if (upCaseTag){ dom.source.wrapSelection('<S>','</S>'); }else{ dom.source.wrapSelection('<s>','</s>'); } // If the user is not in Code view, apply the formatting in Design view. }else if (dw.getFocus() == 'document'){ dom.applyCharacterMarkup("s"); } // Just return--don't do anything else. return;}
- Save the file as Strikethrough.htm in the Configuration/Objects/Text folder.
Instead of including the JavaScript functions in the HEAD
section of the HTML file, you can create a separate JavaScript file. This separate organization is useful for objects that contain several functions, or functions that might be shared by other objects.
To separate the HTML object definition file from the supporting JavaScript functions:
- Create a new blank file.
- Paste all the JavaScript functions into the file.
- Remove the functions from Strikethrough.htm, and add the JavaScript filename to the
src
attribute of the script tag, as shown in the following example:<html><head><title>Strikethrough</title><script language="javascript" src="Strikethrough.js"></script></head><body></body></html>
- Save the Strikethrough.htm file.
- Save the file that now contains the JavaScript functions as Strikethrough.js in the Configuration/Objects/Text folder.