Quick References
      ADO
      ASP
      CSS2
      HTML
      JavaScript
      Jet SQL
      VBScript
      WML
      WMLScript
      WSH
      XHTML
      XML DOM
      XSLT

Features
      Knowledge Base
      Tutorials

Partners
     ZVON.ORG
     XML
     Planet Source Code
     VisualBuilder
     Web Design
     Your HTML Source
     XML/XSLT Forums
     ASPAlliance
     Scripts
     
     Programmers Heaven
     Tek-Tips Forums
     Developer Fusion
     Code Project




Building a WYSIWYG HTML Editor Using Only JavaScript and HTML



Building our HTML editor

Over the next couple of pages we're going to create our own browser based WYSIWYG HTML editor. I won't look at the entire source code for the editor, but I will go into detail about the most important points. The single HTML file and required images are available as part of the support material for this article. You should download it before continuing.

Firstly, we want to make our HTML editor both look and feel like a "normal" HTML editor, such as Front Page 2000. Using style sheets and events, we can create a set of "buttons" that mimic those found in Windows applications. Our HTML editor will contain several buttons to bold text, insert images, etc. Here's how the image that makes up the bold button looks:

<img alt="Bold" class="butClass" src="bold.gif" onMouseOver="selOn(this)" onMouseOut="selOff(this)" onMouseDown="selDown(this)" onMouseUp="selUp(this)" onClick="doBold()">

It has a class attribute of "butClass", which is defined in the <head> part of our HTML document:

.butClass
{
border: 1px solid;
border-color: #D6D3CE;
}

It also responds to five events, which will be fired by the browser: onMouseOver, onMouseOut, onMouseDown, onMouseUp and onClick. The contents of each of these attributes points to a JavaScript function that is defined at the top of the file. For example, when a user moves his/her mouse over a button, the selOn function is called, passing in the image as an object ("this" represents the control for which the event has been raised):

function selOn(ctrl)
{
ctrl.style.borderColor = '#000000';
ctrl.style.backgroundColor = '#B5BED6';
ctrl.style.cursor = 'hand';
}

The selOn function sets the border color of the image to black, its background color to "FrontPage blue", and the mouse cursor to a hand.

Each button in our HTML editor responds to the same five events in exactly the same way. The only difference is that when the onClick event is fired (the user clicks on a button), then each button will call a different JavaScript function, thus performing a different task.

Here's how the buttons will look at different stages:

  The bold button at different stages

Besides buttons, our HTML editor also contains an inline frame (<iframe>) and drop down lists to set the font type and size, as well as various heading sizes (<h1>, <h2>, etc). If you've downloaded the support material, you will see that our HTML editor looks like this in IE:

Our HTML editor in IE

It’s amazing what you can do with a little bit of CSS and some positioning isn't it? When each button is clicked, its onClick event is fired. Here's the code for the doBold() JavaScript function, which is called when the bold "button" is clicked on:

function doBold()
{
iView.document.execCommand('bold', false, null);
}

As you can see, we've used the same execCommand that we used earlier to test bolding some text. In fact, most of our JavaScript functions only contain one line, which is the call to the inline frame's execCommand method.

The code for the italic and underline buttons is fairly similar to the bold buttons code, so let's look at the code for the center align function, doCenter():

function doCenter()
{
iView.document.execCommand('justifycenter', false, null);
}

One again, a simple call to execCommand, passing in the command "justifycenter". To actually test the justifycenter command, click on the inline frame, type in some text, select it, and click the center align button, like this:

The insertorderedlist and insertunorderedlist commands can be passed to execCommand to toggle whether or not the selected text is part of either an ordered or unordered list respectively. When the user clicks on the bulleted (unsorted) list button, the doBulList() JavaScript function is called. It looks like this:

function doBulList()
{
iView.document.execCommand('insertunorderedlist', false, null);
}

Here's a screen shot from me playing around with an unordered list. I've also changed the colors of the foreground and background, which we will look at next:

 


 
  12 3 4 5 6 7
 
   
Copyright 1999-2005 by Infinite Software Solutions, Inc. All rights reserved.
Trademark Information
knoxville photographer
knoxville wedding photographer