04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 13: Scripting Forms<br />

Note that even when commands are supported across all browsers, the HTML that the commands<br />

produce is often very different. For instance, applying the bold command surrounds text with < strong ><br />

in IE and Opera, with < b > in Safari and Chrome, and with a < span > in Firefox. You cannot rely on<br />

consistency in the HTML produced from a rich text editor, due to both command implementation and<br />

the transformations done by innerHTML .<br />

There are some other methods related to commands. The first is queryCommandEnabled() , which<br />

determines if a command can be executed given the current text selection or caret position. This method<br />

accepts a single argument, the command name to check, and returns true if the command is allowed<br />

given the state of the editable area, or false if not. Consider this example:<br />

var result = frames[“richedit”].document.queryCommandEnabled(“bold”);<br />

This code returns true if the “ bold ” command can be executed on the current selection. It ’s worth<br />

noting that queryCommandEnabled() does not indicate if you are allowed to execute the command, but<br />

only if the current selection is appropriate for use with the command. In Firefox, queryCommandEnabled<br />

( “ cut “ ) returns true even though it isn ’ t allowed by default.<br />

The queryCommandState() method lets you determine if a given command has been applied to the<br />

current text selection. For example, to determine if the text in the current selection is bold, the following<br />

can be used:<br />

var isBold = frames[“richedit”].document.queryCommandState(“bold”);<br />

If the “ bold ” command was previously applied to the text selection, then this code returns true . This is<br />

the method by which full - featured rich text editors are able to update buttons for bold, italic, and so on.<br />

The last method is queryCommandValue() , which is intended to return the value with which a<br />

command was executed (the third argument in execCommand in the earlier example). For instance, a<br />

range of text that has the “ fontsize ” command applied with a value of 7 returns “ 7 ” from the<br />

following:<br />

var fontSize = frames[“richedit”].document.queryCommandValue(“fontsize”);<br />

This method can be used to determine how a command was applied to the text selection, allowing you<br />

to determine whether the next command is appropriate to be executed.<br />

461

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!