In this section we will write functions for handling the
events raised by the user. First, we will define global variables. Each of
these variables has its own functionality, but most are created to store a
value indicating if an icon was previously clicked or no by default its value
is equal to No. Only one variable has another purpose, this variable is called
(sourceText) and will be equal to the text entered by the user.
var sourceText= '';
var imgStatusBold = 'No';
var imgStatusItalic = 'No';
var imgStatusUnderLine = 'No';
var imgStatusLeft = 'No';
var imgStatusCenter = 'No';
var imgStatusRight = 'No';
var imgStatusRemoveF= 'No';
var imgStatusWCount = 'No';
var imgStatusInsertL = 'No';
Based on the value of the above variables, when the user
clicks on an icon, if the specific variable's value is equal to “Yes” it indicates
that this icon is not as the equal to the one loaded when the user control has
been loaded and then directly we change the icon to its first status and set
its variable to “No.” If the value was “No” then we will change the icon and
its proper variable to “Yes.”
Three common functions are called onmouseover,
onmouseup, and onclick. Onmouseover, a function (ChangeImg()), is used to change
the icon when the user places his mouse at a certain icon. This function takes
2 parameters (icon id and the new icon name). Note that the toolbar icons are
contained in a folder called "Images" under the root element of the
function ChangeImg(id, imgsrc)
var imgSrc = "Images/" + imgsrc;
document.getElementById(id).src = imgSrc;
Onmouseup function is used to
return the icon to its previous state when the mouse leaves a specific icon; it
takes 3 parameters (icon id, new icon name, and a variable).
function ReturnImg(id, imgsrc, ownVar)
if(ownVar == 'No')
var imgSrc = "Images/" + imgsrc;
document.getElementById(id).src = imgSrc;
The third parameter is used to indicate either this icon was
selected or not. Its default value is No.
Onclick function is called to perform a specific action. A
The above method is used to align the text entered by the
user to the left. ExecCommand is used to execute a
command on a document. It consists of predefined functions which are used to
manipulate the page layout directly into the browser. Do not use it until the
page has finished loading.
function MakeBold(boldover, bold)
var img = document.getElementById('Bold')
var imgBold = "Images/" + boldover
var imgNotBold = "Images/" + bold
img.src = imgBold;
if(imgStatusBold == 'Yes')
img.src = "Images/" + bold;
The function above is used to format the text entered by the
user as bold. When he clicks on this icon, we should change it to another one.
Set the global variable for this action to “Yes” which means that it has been
selected. We will use this variable each time the user clicks on it to see if
it has been previously selected or no, and then we will make the transformation
to the text into bold. It takes 2 parameters, the first is to store the
bold-over image location (url) and the second is to store the bold image
location (url). The same concept is used to make the text Italic and
To apply alignments into the text, we use the same as the
MakeBold() function, but instead, the 3 alignment functionalities work in a
parallel way which means that if a specific alignment is selected all others
will be unselected and will retain their original status.
For a complete reference about the execCommand command list
Now we will create a function that counts how many words the
user has entered. A variable is defined with default value equal to 0. Another
variable is defined that holds the text entered. We will split the spaces in
this variable, add the splitting results to an array and check each element
length in the array. If it is equal to zero then it is a space, else add 1 to
the specific variable. After this stage we will set the value of the textbox to
the count variable.
var wordCount = document.frames['HamHtmlEditor1_content'].document.body.innerText;
var count = 0;
countWithSpace = wordCount.replace('\n', '');
countWithoutSpaces = countWithSpace.split(' ');
window.parent.document.getElementById('HamHtmlEditor1_TxtCount').value = count;
When the "Remove Formatting" image is clicked, we
execCommand to remove all the formats applied to the text. It is very useful if
a user wants to remove all the formats that he already applied into the text.
function, but they send "undo" or "redo" to the execCommand
to undo or redo the user action. It takes 1 parameter which is the execCommand
function Formats (style)
When the user clicks on "Insert emoticons" icon,
we will show the third div layer placed directly after this icon which contains
var div = document.getElementById(images);
if(div.style.display == 'block')
div.style.display = 'none';
div.style.display = 'block';
Figure2: Shows the editor when the "insert emoticons"
When the user clicks on any emoticon inside the third div
the cursor back to the editor. This method takes 3 parameters. The first
parameter is passed to execCommand that inserts the image into the document
(insertimage), the second is the image location (url), and the third is the id
of the div layer. Note that the emoticons are placed inside a folder called
"Emoticons" under the root element of the web application.
document.getElementById(images).style.display = 'none'
When the "View as HTML"
image is clicked, the second div layer is shown and the first layer is hidden.
The "readonly" property of this div layer is set to "true"
which means that the user at runtime cannot edit it, then we will get the HTML
code and insert it into that div layer.
document.getElementById('HamHtmlEditor1_Div2').style.display = 'block';
When the "Design view"
image is clicked, the third div layer (containing the HTML code) is just
document.getElementById('HamHtmlEditor1_Div2').style.display = 'none';
The dropdown lists are used to let the user selects the
predefined data entered to manipulate the text.
The first one is used to change the font size; sizes may
vary from 1 (10 pt) to 7 (22 pt). We will handle the onclick event by calling a
list and sends it as a third parameter to the execCommand command. The first
parameter is the FontSize, in which you are telling this command to execute the
FontSize function with its specific size. Note that the font sizes in the
dropdown list are predefined in the user control HTML code created earlier.
var fontSize = document.getElementById('FontDropDownonchange');
The code below is used to change the font name; the first
parameter of the execCommand command takes fontname as first parameter, the
second is false or 0, and the third is the user’s selection.
var fontName = document.getElementById("FontFamilyName");
The third is used to change the font color; the colors are
predefined so if you want to add more colors you can modify the HTML code of
this dropdown list.
var fontColor = document.getElementById("Color");
The final function will assign the text entered by the user
to a hidden input HTML element and store its specific HTML code to another
hidden HTML element. It is called when the editor loses the focus (onblur)
event of the iframe containing the HTML page. In this way, we can use the two
properties created as you will see in the next section to get the hidden inputs
values from the code behind.
file ready; all you need to do now is to link it to the page where you want to
use the user control by adding the below code in the <head> tag.
add the word "debugger;" at the beginning of the code. In this way
the compiler will start the debugging mode. Or you can go to your internet
explorer options, navigate to Advanced, unselect the disable script debugging
(internet explorer), unselect disable script debugging (other) and now add your