// Cross-Browser Rich Text Editor // http://www.kevinroth.com/rte/demo.htm // Written by Kevin Roth (kevin@NOSPAMkevinroth.com - remove NOSPAM) // Visit the support forums at http://www.kevinroth.com/forums/index.php?c=2 // This code is public domain. Redistribution and use of this code, with or without modification, is permitted. //init variables var isRichText = false; var rng; var currentRTE; var allRTEs = ""; var isIE; var isGecko; var isSafari; var isKonqueror; var imagesPath; var includesPath; var cssFile; var generateXHTML; var lang = "en"; var encoding = "iso-8859-1"; function initRTE(imgPath, incPath, css, genXHTML) { //set browser vars var ua = navigator.userAgent.toLowerCase(); isIE = ((ua.indexOf("msie") != -1) && (ua.indexOf("opera") == -1) && (ua.indexOf("webtv") == -1)); isGecko = (ua.indexOf("gecko") != -1); isSafari = (ua.indexOf("safari") != -1); isKonqueror = (ua.indexOf("konqueror") != -1); generateXHTML = genXHTML; //check to see if designMode mode is available //Safari/Konqueror think they are designMode capable even though they are not if (document.getElementById && document.designMode && !isSafari && !isKonqueror) { isRichText = true; } if (isIE) { document.onmouseover = raiseButton; document.onmouseout = normalButton; document.onmousedown = lowerButton; document.onmouseup = raiseButton; } //set paths vars imagesPath = incPath + imgPath; includesPath = incPath; cssFile = css; if (isRichText) document.writeln(''); //for testing standard textarea, uncomment the following line //isRichText = false; } function writeRichText(rte, html, width, height, buttons, readOnly) { if (isRichText) { if (allRTEs.length > 0) allRTEs += ";"; allRTEs += rte; if (readOnly) buttons = false; //adjust minimum table widths if (isIE) { if (buttons && (width < 540)) width = 600; var tablewidth = width; } else { if (buttons && (width < 540)) width = 600; var tablewidth = width + 4; } document.writeln('
'); if (buttons == true) { document.writeln(''); document.writeln(' '); document.writeln(' '); document.writeln(' '); document.writeln(' '); document.writeln(' '); document.writeln(' '); document.writeln('
'); document.writeln(' '); document.writeln(' '); document.writeln(' '); document.writeln(' '); document.writeln(' '); document.writeln(' '); document.writeln('
'); document.writeln(''); document.writeln(' '); document.writeln(' '); document.writeln(' '); document.writeln(' '); document.writeln(' '); document.writeln(' '); document.writeln(' '); document.writeln(' '); document.writeln(' '); document.writeln(' '); document.writeln(' '); document.writeln(' '); document.writeln(' '); document.writeln(' '); document.writeln(' '); document.writeln(' '); document.writeln(' '); document.writeln(' '); document.writeln(' '); document.writeln(' '); document.writeln(' '); document.writeln(' '); document.writeln(' '); //if (isIE) { // document.writeln(' '); //} // document.writeln(' '); // document.writeln(' '); // document.writeln(' '); // document.writeln(' '); // document.writeln(' '); // document.writeln(' '); // document.writeln(' '); document.writeln(' '); document.writeln(' '); document.writeln('
BoldItalicUnderlineAlign LeftCenterAlign RightJustify FullHorizontal RuleOrdered ListUnordered ListOutdentIndent
Text Color
Background Color
Insert LinkAdd Image
Insert Table
Spell CheckCutCopyPasteUndoRedo
'); } document.writeln(''); //if (!readOnly) document.writeln('
 '); document.writeln(''); document.writeln(''); document.writeln('
'); document.getElementById('hdn' + rte).value = html; enableDesignMode(rte, html, readOnly); } else { if (!readOnly) { document.writeln(''); } else { document.writeln(''); } } } function enableDesignMode(rte, html, readOnly) { var frameHtml = "\n"; frameHtml += "\n"; //to reference your stylesheet, set href property below to your stylesheet path and uncomment if (cssFile.length > 0) { frameHtml += "\n"; } else { frameHtml += "\n"; } frameHtml += "\n"; frameHtml += "\n"; frameHtml += html + "\n"; frameHtml += "\n"; frameHtml += ""; if (document.all) { var oRTE = frames[rte].document; oRTE.open(); oRTE.write(frameHtml); oRTE.close(); if (!readOnly) { oRTE.designMode = "On"; frames[rte].document.attachEvent("onkeypress", function evt_ie_keypress(event) {ieKeyPress(event, rte);}); } } else { try { if (!readOnly) document.getElementById(rte).contentDocument.designMode = "on"; try { var oRTE = document.getElementById(rte).contentWindow.document; oRTE.open(); oRTE.write(frameHtml); oRTE.close(); if (isGecko && !readOnly) { //attach a keyboard handler for gecko browsers to make keyboard shortcuts work oRTE.addEventListener("keypress", geckoKeyPress, true); } } catch (e) { alert("Error preloading content."); } } catch (e) { //gecko may take some time to enable design mode. //Keep looping until able to set. if (isGecko) { setTimeout("enableDesignMode('" + rte + "', '" + html + "', " + readOnly + ");", 10); } else { return false; } } } } function updateRTE(rte) { if (!isRichText) return; //check for readOnly mode var readOnly = false; if (document.all) { if (frames[rte].document.designMode != "On") readOnly = true; } else { if (document.getElementById(rte).contentDocument.designMode != "on") readOnly = true; } if (isRichText && !readOnly) { //if viewing source, switch back to design view //if (document.getElementById("chkSrc" + rte).checked) document.getElementById("chkSrc" + rte).click(); setHiddenVal(rte); } } function setHiddenVal(rte) { //set hidden form field value for current rte var oHdnField = document.getElementById('hdn' + rte); //convert html output to xhtml (thanks Timothy Bell and Vyacheslav Smolin!) if (oHdnField.value == null) oHdnField.value = ""; if (document.all) { if (generateXHTML) { oHdnField.value = get_xhtml(frames[rte].document.body, lang, encoding); } else { oHdnField.value = frames[rte].document.body.innerHTML; } } else { if (generateXHTML) { oHdnField.value = get_xhtml(document.getElementById(rte).contentWindow.document.body, lang, encoding); } else { oHdnField.value = document.getElementById(rte).contentWindow.document.body.innerHTML; } } //if there is no content (other than formatting) set value to nothing if (stripHTML(oHdnField.value.replace(" ", " ")) == "" && oHdnField.value.toLowerCase().search("