Appendix D. Internet Explorer Commands

Internet Explorer 4 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. Even so, 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 TextRangeobject 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 Text-Range 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 ...

Get Dynamic HTML: The Definitive Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.