execCommand method

Executes a command on the current document, current selection, or the given range.

Syntax

*object.*execCommand(cmdID, showUI, value)

Parameters

cmdID [in]

Type: String

String that specifies the command to execute. This command can be any of the command identifiers that can be executed in script.

showUI [in, optional]

Type: Boolean

Boolean that specifies one of the following values.

false (false)

Default. Do not display a user interface. Must be combined with value, if the command requires a value.

true (true)

Display a user interface if the command supports one.

value [in, optional]

Type: Variant

Variant that specifies the string, number, or other value to assign. Possible values depend on the command.

Return value

Type: Boolean

Returns true if the command is successful.

Standards information

There are no standards that apply here.

Remarks

Do not invoke the execCommand method until after the page loads.

The showUI and value parameters might be required depending on the command being executed.

This method is a wrapper function for the Exec method to enable scriptable execution of some command constants. Exec provides better performance and greater versatility than this method. You can obtain an IOleCommandTarget interface by calling QueryInterface on the IHTMLDocument2 interface using IID_IOleCommandTarget.

This method is a wrapper function for the Exec method to enable scriptable execution of some command constants. Exec provides better performance and greater versatility than this method. You can obtain an IOleCommandTarget interface by calling QueryInterface on the IHTMLControlRange interface using IID_IHTMLControlRange for the IID.

This method is a wrapper function for the Exec method to enable scriptable execution of some command constants. Exec provides better performance and greater versatility than this method. You can obtain an IOleCommandTarget interface by calling QueryInterface on the IHTMLTxtRange interface using IID_IHTMLTxtRange for the IID.

Examples

The following example shows how to use the IDM_HYPERLINK constant as the cmdID of the execCommand method to allow the user to create a hyperlink from selected text. The scriptJScript then retrieves the specified URL and uses it to replace the selected text.

Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/execCommand_CreateLink.htm

<script type="text/javascript">
function AddLink()
{
    //identify selected text
    var sText = document.selection.createRange();
    if (sText.text != "")
    {
      //create link
      document.execCommand("CreateLink");
      //change the color to indicate success
      if (sText.parentElement().tagName == "A")
      {
        sText.execCommand("ForeColor",false,"#FF0033");
      }
    }
    else
    {
        alert("Please select some text!");
    }   
}
</script>
<p unselectable="on">Select any portion of the following blue text, such as &quot;My 
favorite Web site&quot;. Click the button to turn the selected text into a link. </p>
<p style="color= #3366CC">My favorite Web site is worth clicking on. Don't forget 
to check out my favorite music group!</p>
<button onclick="AddLink()" unselectable="on">Click to add link</button>

See also

controlRange

document

TextRange

Reference

queryCommandEnabled

queryCommandIndeterm

queryCommandState

queryCommandSupported

queryCommandValue

Conceptual

Command Identifiers