Internet Explorer includes a set of commands that work directly with the document and (Win32 only) TextRange objects. In many cases, these commands mimic the functionality available through setting properties or invoking methods of the objects. Some of the newer commands operate within the context of the MSHTML Edit Mode. All of these commands exist outside of the primary document object model and are therefore treated separately in this appendix.
Access to these commands is through a set of document and TextRange object methods that are described in Chapter 9. These commands and syntax are:
execCommand("commandName"[, UIFlag[, value]]) queryCommandEnabled("commandName") queryCommandIndeterm("commandName") queryCommandState("commandName") queryCommandSupported("commandName") queryCommandText("commandName")
This appendix focuses on the commands and values that may be applied to the execCommand( ) method (the commands may also be applied to the other methods).
Some commands work on the current selection in a document, which means that the selection must be made manually by the user or via a script and the TextRange object. For example, the following function locates every instance of a string passed as a parameter and turns its text color to red:
function redden(txt) { var rng = document.body.createTextRange( ); for (var i = 0; rng.findText(txt) != false; i++) { rng.select( ); document.execCommand("ForeColor","false","red") ; rng.collapse(false); rng.select( ); } }
The process is iterative. After creating a text range for the entire document body, the function repeatedly looks for a match of the string. Whenever there is a match, the matched word is selected, and the execCommand( ) method invokes the ForeColor command, passing the value red as the color. To continue searching through the range, the range is collapsed after the previously found item, and the selection is removed (by selecting a range of zero length).
In general, I recommend using a regular object model method or property setting when one exists for the action you wish to take. Because these commands tend to work only with IE on Win32 operating systems, you may be forced to avoid them if your audience has a wider browser base.
Command |
Description |
Parameter |
---|---|---|
2D-Position |
Makes absolute-positioned element inherently draggable (IE 5.5 or later) |
Boolean to enable (true) or disable (false) dragging |
AbsolutePosition |
Sets style.position property to absolute (IE 5.5 or later) |
Boolean to enable (true) or disable (false) setting |
BackColor |
Sets background color of current selection |
Color value (name or hex triplet) |
Bold |
Wraps a <b> tag around the range |
None |
Copy |
Copies the range to the Clipboard |
None |
CreateBookmark |
Wraps an <a name=> tag around the range or modifies an existing <a> tag |
A string of the anchor name; tag is removed if value is omitted |
CreateLink |
Wraps an <a href=...> tag around the current selection |
A string of a complete or relative URL |
Cut |
Copies the range to the Clipboard, then deletes range |
None |
Delete |
Deletes the range |
None |
FontName |
Sets the font of current selection |
A string of the face attribute |
FontSize |
Sets the font size of current selection |
A string of the font size |
ForeColor |
Sets the foreground (text) color of current selection |
Color value (name or hex triplet) |
FormatBlock |
Wraps a block tag around the current object |
Unknown |
Indent |
Indents current selection |
None |
InsertButton |
Inserts a <button> tag at current insertion point |
A string for the element ID |
InsertFieldset |
Inserts a <fieldset> tag at current insertion point |
A string for the element ID |
InsertHorizontalRule |
Inserts <hr> at current insertion point |
A string of the rule size (not working) |
InsertIFrame |
Inserts an <iframe> tag at current insertion point |
A string of a URL for the src property |
InsertImage |
Inserts an <img> tag at current text selection (IE 5 or later) |
A string of a URL for the src property |
InsertInputButton |
Inserts an <input type="button"> tag at current insertion point |
A string for the element ID |
InsertInputCheckbox |
Inserts an <input type="checkbox"> tag at the current insertion point |
A string for the element ID |
InsertInputFileUpload |
Inserts an <input type="file"> tag at the current insertion point |
A string for the element ID |
InsertInputHidden |
Inserts an <input type="hidden"> tag at current insertion point |
A string for the element ID |
InsertInputImage |
Inserts an <input type="image"> tag at current insertion point |
A string for the element ID |
InsertInputPassword |
Inserts an <input type="password"> tag at current insertion point |
A string for the element ID |
InsertInputRadio |
Inserts an <input type="radio"> tag at current insertion point |
A string for the element ID |
InsertInputReset |
Inserts an <input type="reset"> tag at current insertion point |
A string for the element ID |
InsertInputSubmit |
Inserts an <input type="submit"> tag at current insertion point |
A string for the element ID |
InsertInputText |
Inserts an <input type="text"> tag at current insertion point |
A string for the element ID |
InsertMarquee |
Inserts a <marquee> tag at current insertion point |
A string for the element ID |
InsertOrderedList |
Inserts an <ol> tag at current insertion point |
A string for the element ID |
InsertParagraph |
Inserts a <p> tag at current insertion point |
A string for the element ID |
InsertSelectDropdown |
Inserts a <select> tag whose type is select-one at current insertion point |
A string for the element ID |
InsertSelectListbox |
Inserts a <select> tag whose type is select-multiple at current insertion point |
A string for the element ID |
InsertTextArea |
Inserts a <textarea> tag at current insertion point |
A string for the element ID |
InsertUnorderedList |
Inserts a <ul> tag at current insertion point |
A string for the element ID |
Italic |
Wraps an <i> tag around the range |
None |
JustifyCenter |
Full justifies the current selection |
None |
JustifyLeft |
Left justifies the current selection |
None |
JustifyRight |
Right justifies the current selection |
None |
LiveResize |
Dynamically refresh element resizing in edit mode (IE 5.5 or later) |
Boolean to enable (true) or disable (false) setting |
MultipleSelection |
Allow multiple element selections in edit mode (IE 5.5 or later) |
Boolean to enable (true) or disable (false) setting |
Outdent |
Outdents the current selection |
None |
OverWrite |
Sets the input-typing mode to overwrite or insert |
Boolean (true if mode is overwrite) |
Paste |
Pastes contents of the Clipboard at current insertion point or over the current selection |
None |
Displays Print dialog box (IE 5.5 or later) |
None |
|
Refresh |
Reloads the current document |
None |
RemoveFormat |
Removes formatting from current selection |
None |
SaveAs |
Saves the page as a local file (optional file dialog) |
A string of a URL for the path |
SelectAll |
Selects entire text of the document |
None |
UnBookmark |
Removes anchor tags from the selection or text range |
None |
Underline |
Wraps a <u> tag around the range |
None |
Unlink |
Removes a link from the selection or text range |
None |
Unselect |
Clears a selection from the document |
None |
Copyright © 2003 O'Reilly & Associates. All rights reserved.