Using execCommand


Using execCommand

The Internet Explorer actually supports more methods than are normally available through JavaScript, but you can reach many of them with the execCommand method of the document object. (Some commands are available only through ActiveX programming, for example.) This method works with the document, a current selection, or a text range. (We'll see text ranges in Chapter 11.)

Here's the syntax for this method: execCommand( Command [, UserInterface ] [, Value ]) , where Command is a string that specifies the command to execute. UserInterface is a Boolean that you set to false (the default) if you do not want to display a user interface, and true if you do. Value is a string or number, or other value to assign. This method returns true if successful, false otherwise . You can see the possible commands to use in Table 9.6. (More commands are available, but they're not yet supported by the Internet Explorer.)

Table 9.6. The Commands of the execCommand Method

Command

Does This

2D-Position

Drags positioned elements.

AbsolutePosition

Sets an element's position to "absolute."

BackColor

Sets or gets the background color of a selection.

Bold

Toggles the current selection between bold and plain (unbold) text.

Copy

Copies the selection to the clipboard.

CreateBookmark

Creates a bookmark anchor.

CreateLink

Adds a hyperlink to the current selection.

Cut

Deletes the current selection, after copying it to the clipboard.

Delete

Deletes the current selection.

FontName

Sets or gets the font for the current selection.

FontSize

Sets or gets the font size for the current selection.

ForeColor

Sets or gets the text color of the current selection.

Indent

Increases the indent of the current selection by one indentation unit.

InsertButton

Places a button control over a text selection.

InsertHorizontalRule

Places a horizontal line over a text selection.

InsertIFrame

Places an inline frame over a text selection.

InsertImage

Places an image over a text selection.

InsertInputButton

Places an input button over a text selection.

InsertInputCheckbox

Places a check box over a text selection.

InsertInputFileUpload

Places a file input control over a text selection.

InsertInputHidden

Places a hidden HTML control over a text selection.

InsertInputImage

Places an image control over a text selection.

InsertInputPassword

Places a password control over a text selection.

InsertInputRadio

Places a radio button over a text selection.

InsertInputReset

Places a reset button over a text selection.

InsertInputSubmit

Places a submit button over a text selection.

InsertInputText

Places a text field over a text selection.

InsertMarquee

Places an Internet Explorer marquee element ( <MARQUEE> ) over a text selection.

InsertOrderedList

Changes the format of the current selection from an ordered list and normal text format.

InsertParagraph

Places a paragraph over a text selection.

InsertSelectDropdown

Places a drop-down control over a text selection.

InsertSelectListbox

Places a select box over a text selection.

InsertTextArea

Places a text area over a text selection.

InsertUnorderedList

Places an unordered list over a text selection.

Italic

Toggles the current selection between italic and plain (nonitalic) text.

JustifyCenter

Centers the current selection.

JustifyLeft

Left-justifies the current selection.

JustifyRight

Right-justifies the current selection.

MultipleSelection

Enables the user to select more than one element at a time (with the use of the user holds down the Shift or Ctrl keys in Windows, for example).

Outdent

Decreases indentation of the current selection by one increment unit.

OverWrite

Toggles mode between insert and overwrite text-entry mode.

Paste

Pastes the current selection to the clipboard.

Print

Prints the current page.

Refresh

Refreshes the document.

SaveAs

Saves the web page to a file.

SelectAll

Selects an entire document.

UnBookmark

Removes all bookmarks from the current selection.

Underline

Toggles the current selection between underlined and plain (not underlined ) text.

Unlink

Removes all hyperlinks from the current selection.

Unselect

Unselects the current selection.

To create a new hyperlink from the current selection in a web page, for example, you can use the "CreateLink" command. Here's an example that does exactly thatwhen you select some text in this web page and click the button, that text is converted into a hyperlink:

(Listing 09-08.html on the web site)
 <HTML>      <HEAD>          <TITLE>Creating a link</TITLE>          <SCRIPT LANGUAGE="JavaScript">              <!--  function createLink()   {   document.execCommand("CreateLink")   }  // -->          </SCRIPT>      </HEAD>      <BODY>          <H1>Creating a link</H1>  Here's some text. Select some and click the button to turn it into a hyperlink.   <BR>   <BUTTON ONCLICK="createLink()">Click to create the link</BUTTON>  </BODY>  </HTML> 

You can see the results in Figure 9.5, where I've selected some text and clicked the button, opening a dialog box where I've filled in information about the hyperlink.

Figure 9.5. Creating a new hyperlink.

graphics/09fig05.gif

When you close the dialog box, the new hyperlink appears, as you see in Figure 9.6note the URL of the new hyperlink, displayed in the status bar, is the URL I set in the hyperlink dialog box.

Figure 9.6. A new hyperlink.

graphics/09fig06.gif



Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net