command element can be used to invoke an operation that can come from multiple sources. For example, a clipboard paste operation can be invoked from the Edit menu, a context menu and by pressing a keyboard shortcut. You attach the code to the command using the
oncommand attribute. It will be called no matter how it is invoked by the user. In addition, disabling the command will automatically disable the menu items and keyboard shortcuts.
Commands are identified by their
. If you include the script
chrome://global/content/globalOverlay.js in your window, you can use the function
goDoCommand function to invoke the command. Using this function has the advantage that the command will be sent to the part of the UI which will respond to it. Typically, this will be the currently focused element.
broadcaster, commands forward attributes to other elements.
The following code will send a paste command (
cmd_paste) to the currently focused element:
// First include chrome://global/content/globalOverlay.js goDoCommand("cmd_paste");
Example with two buttons
<commandset><command id="cmd_openhelp" oncommand="alert('Help');"/></commandset> <button label="Help" command="cmd_openhelp"/> <button label="More Help" command="cmd_openhelp"/>
- Type: boolean
- Indicates whether the element is disabled or not. If this attribute is set, the element is disabled. Disabled elements are usually drawn with grayed-out text. If the element is disabled, it does not respond to user actions, it cannot be focused, and the
commandevent will not fire. In the case of form elements, it will not be submitted. Do not set the attribute to
true, as this will suggest you can set it to
falseto enable the element again, which is not the case.
- Visible controls have a
disabledproperty which, except for menus and menuitems, is normally preferred to use of the attribute, as it may need to update additional state.
- Type: script code
- This event handler is called when the command is activated. This occurs when a user selects a menu item or presses a keyboard shortcut attached to the command.
- Type: string
- This attribute applies to a
- Setting this attribute to "true" indicates that the command is reserved for Chrome code and is not available for use in the content. This means that, to execute these commands, key events won't be passed to content, and event listeners registered for them in content will not be executed.
- Currently, the content still receives these key events, even though it can't override them. In future releases, it won't receive them. See bug 1173061 for details.