execCommand method
The execCommand
method implements multiple different commands. Some
of them provide access to the clipboard, while others are for editing
form inputs,
contenteditable
elements or entire documents (when switched to
design mode).
To access the clipboard, the newer
Clipboard API
is recommended over execCommand()
. However, there is no replacement for
the editing commands: unlike direct DOM manipulation, modifications
performed by execCommand()
preserve the undo buffer (edit history).
Most commands affect the document's
selection.
For example, some commands (bold, italics, etc.) format the currently
selected text, while others delete the selection, insert new elements
(replacing the selection) or affect an entire line (indenting). Only the
currently active editable element can be modified, but some commands (e.g.
copy
) can work without an editable element.
Note: Modifications performed by
execCommand()
may or may not triggerElement.beforeinput_event
andElement.input_event
events, depending on the browser and configuration. If triggered, the handlers for the events will run beforeexecCommand()
returns. Authors need to be careful about such recursive calls, especially if they callexecCommand()
in response to these events. From Firefox 82, nestedexecCommand()
calls will always fail, see bug 1634262.
Implementation
external bool execCommand(
String commandId, [
bool showUI,
String value,
]);