execCommand method in javascript

Cp_raj
Posted by in JavaScript category on for Beginner level | Views : 25869 red flag

This article explains the use of execCommand method used in Javascript that is used to execute a command in JavaScript.


 Download source code for execCommand method in javascript

Introduction

if you are going to design a RichTextBox on your own, then go through execCommand method. It will execute a command on the current document or  the selected range in the document.

Examples


You can allow the user to paste the text from the clipboard using ‘Paste’ command in the execCommand method.

var myRange1 = document.body.createTextRange();

   myRange1.moveToElementText(myDiv2);

   myRange1.execCommand('Paste');

After pasting the data, if you want to delete selected data.Then use ‘Delete’ command in the execCommand method

var cRange = document.selection.createRange();

    cRange.execCommand('Delete');

If the pasted data is bulletlist and if your want to indent the data then use ‘Indent’ command , if you want to outdent then use ‘Outdent’ command.

var cRange = document.selection.createRange();

    cRange.execCommand('Indent');

var cRange = document.selection.createRange();

    cRange.execCommand('Outdent');


Conclusion

execCommand will be used to execute the command for the Current Range, Selected document.


Reference


Like this lots of commands can be used in the execute command, you can have a look at this post http://msdn.microsoft.com/en-us/library/ms533049(v=VS.85).aspx


Please download the Zip and execute the HTML. Select the bulletlist and click paste.

Page copy protected against web site content infringement by Copyscape

About the Author

Cp_raj
Full Name: Rajkumar Pauldurai
Member Level: Starter
Member Status: Member
Member Since: 5/26/2009 6:29:52 AM
Country:

http://www.dotnetfunda.com
I started my career with Asp,ms-access,sql server 7,xml.Then i updated to .net,share point server,Basics about Biztalk server

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)