Using execCommandThe 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
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.
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.
|