Online WYSIWYG HTML Editor's realization Principle analysis

Source: Internet
Author: User

Online WYSIWYG HTML Editor's realization Principle analysis

This article mainly introduces the realization principle of the WYSIWYG HTML editor on line, this article uses initialization, open editing function, get editor content, add style setting, further and so on to explain the basic realization principle of online editor, need friend can refer to

Web site development is increasingly advocating the user experience, to provide users with more convenient tools, and online HTML content editor is one of the more "old" one. Simple features can provide users with style control of text, such as the color of text, font size, and so on, and the complex can even provide the same powerful features like word. Although there are a lot of open source editors, but the real use is not much, so their improvement work has been in progress.

Today, most editors on the Web have very powerful functions, relatively speaking, in the use of a lot of configuration, of course, the code will naturally be more "bloated." If we don't need a powerful editor, we can implement one ourselves because the code is not complex. Here is a little personal experience for reference only (take ExtJS's htmleditor as an example).

1, initialization. When the page has finished loading, add an iframe to the page (optional). Here to note that, to determine the status of the page, you have to wait for the page completely loaded after the operation, to prevent the occurrence of some elements can not find errors.

2, open the editing function. Set the IFRAME as editable (the following code comes from the ExtJS htmleditor):

The code is as follows:

Get the Window object for the IFRAME

Getwin:function () {

Return Ext.isie? This.iframe.contentwindow:window.frames[];


Get the Document object for the IFRAME

Getdoc:function () {

Return Ext.isie? This.getwin (). Document: (This.iframe.contentDocument | | this.getwin (). Document);


Opens the Document object, writes the initialization content to it, to be compatible with Firefox

doc = This.getdoc (); ();

Doc.write (" );

Open Document Object Editing mode

Doc.designmode = "on";

Doc.close ();

This allows you to write to this simple editor.

3, get the contents of the editor, the code is as follows:

Copy code code as follows:

Get the body object for the editor

var BODY = Doc.body | | Doc.documentelement;

Get the contents of the editor

var content = body.innerhtml;

Handle content, such as replacing some of the special characters, and so on

Some Code

Back to Content

return content;

4, add style settings. Although the above editor implements the basic function, but it is a bit too simple, should add some simple style implementation. The ExecCommand method of document makes this idea possible.

The code is as follows:

Unified Execution Command method

function ExecCmd (cmd, value) {

Doc object gets reference to the above code

Calling the ExecCommand method to execute a command

Doc.execcommand (cmd, false, value = = undefined? null:value);


Change the selected font to bold, ctrl-b

ExecCmd (' bold ');

Underline, Ctrl-u

ExecCmd (' underline ');

into italics, ctrl-i.

ExecCmd (' italic ');

Set the color of text

ExecCmd (' ForeColor ', Ext.issafari | | Ext.isie? ' # ' +color:color);

Insert a piece of content at the cursor

function Insertatcursor (text) {

Win object's get reference to the above code

if (Ext.isie) {

Win.focus ();

var r = Doc.selection.createRange ();

if (r) {

R.collapse (TRUE);

r.pastehtml (text); }

}else if (Ext.isgecko | | Ext.isopera) {

Win.focus ();

ExecCmd (' inserthtml ', text);

}else if (Ext.issafari) {

ExecCmd (' InsertText ', text);



5, further. Now that you can change the style, if the editor has a toolbar (which should be inevitable), then we also want the buttons on the toolbar to be automatically highlighted or displayed according to the style of the cursor position. The Querycommandstate () method of document makes this idea possible.

The code is as follows:

Doc object's get reference to the opposite above

Whether the cursor is bold

var isbold = doc.querycommandstate (' bold ');

if (isbold) {

Change the style of the Bold button


Of course, the above code can be merged, here is just a schematic


Doc.querycommandstate (' underline ');

Italic body

Doc.querycommandstate (' italic ');

This article simply provides a simple way to implement the editor, some of which can be used directly. Suggestion, the friend that wants to realize editor by oneself can refer to the HTMLEditor code in the next ExtJS, both simple and clearer, can expand on it.

One last caveat: Be sure to be aware of browser compatibility issues, and don't wait until the end of the test compatibility, for such a large number of JavaScript code, adjustment is more painful things.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.