Microsoft's Dynamic HTML Editing Component (DEC)
There are actually two different ways you can accomplish HTML editing in IE5 and up. There is the MSHTML editor and then there's the DHTML Editing Component. The MSHTML editor allows for WYSIWYG editing by defining "editable regions" of a page. The DEC on the other hand is an embedded ActiveX control that works much like a <textarea> to edit HTML and also works in IE4.
What's the difference? The MSHTML editor is the core of the technology and the DEC is just an Active-X wrapper that uses the MSHTML editor. The DEC adds extra functionality (like adding tables) that isn't available by default with the MSHTML editor -- for this reason the DEC is still used quite a bit.
So, how do you use this thing? It's really quite simple... just drop in this piece of code where you want it to exist on the page:
<object ID="tbContentElement" CLASS="tbContentElement" CLASSID="clsid:2D360201-FFF5-11D1-8D03-00A0C959BC0A" VIEWASTEXT width="450" height="300"> <param name=Scrollbars value=true> </object>
There are actually two versions of the ActiveX control. One is safe and the other is not safe. Why are there two different versions? With one, you can access files off the hard drive or other Web sites. But in order to be able to do that, the user has to have their security settings set low enough to permit such a thing or they will be prompted that the control is not safe and asked to continue. The code above uses the safe version of the control and therefore cannot load external documents. If you wish to use the non-safe version, just replace the CLASSID to: clsid:2D360200-FFF5-11D1-8D03-00A0C959BC0A.
Now, why use the safe version? Because it works really well in a database-driven application. Content can be copied into and out of the control using script. With this additional code we copy the contents of a textarea into our control:
You should now see the content from the textarea appear in the control! It's really easy then to go from here and pull the content from a database and input it into the control. You would just perform your database query and place the content into the textarea.
Example: <textarea ...><%=htmlcontent%></textarea>
Let's say that the user has entered some content into the editing control and now wants to save the information to the database. We need to copy the content from the control back into our textarea to be submitted to our database. This is done in just the exact opposite way of putting the content into the control:
I've placed the code into a function that will get called when you go to submit the form to the database. To put it all together we get this:
You may notice in our final code a couple minor differences:
- The <object> code is now contained with the form tags; and
- the reference to the editor has document.all attached to the front of it.
So, we've discovered how to use the control and how to move content between the textarea and the control.