www.fgks.org   »   [go: up one dir, main page]

Click to Rate and Give Feedback
Methods
 execCommand Method
execCommand Method

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

Syntax

bSuccess = object.execCommand(sCommand [, bUserInterface] [, vValue])

Parameters

sCommand Required. String that specifies the command to execute. This command can be any of the command identifiers that can be executed in script.
bUserInterface Optional. Boolean that specifies one of the following values.
falseDefault. Do not display a user interface. Must be combined with vValue , if the command requires a value.
trueDisplay a user interface if the command supports one.
vValue Optional. Variant that specifies the string, number, or other value to assign. Possible values depend on the command.

Return Value

Returns true if the command is successful.

Remarks

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

The bUserInterface and vValue parameters might be required depending on the command being executed.

Example

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

<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  unselectable="on">Click to add link</button>
This feature requires Microsoft® Internet Explorer 4.0 or later. Click the following icon to install the latest version. Then reload this page to view the sample.

Standards Information

There is no public standard that applies to this method.

Applies To

document, controlRange, TextRange

See Also

Command Identifiers, queryCommandEnabled, queryCommandIndeterm, queryCommandState, queryCommandSupported, queryCommandValue
Tags What's this?: Add a tag
Community Content
 
Add Community Content
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker
DCSIMG