Since I learned about regular expressions, I developed some JavaScript apps in order to allow me to make massive replacements in texts, something like Unix vi editor. Internet Explorer has given a lot of problems regarding the cursor position and textarea undo/redo. About the las one I found some facts affecting this feature:
Modifying, with a script, the textarea content with a range selection; eg:
Var textElement = document.getElementById(‘editText’);
textElement.focus();
var sep = '\001';
var range = document.selection.createRange(); //create selection range
var copyRange = range.duplicate();
copyRange.setEndPoint('StartToEnd',range); //select end of range
copyRange.text = sep;
endPos = textElement.value.indexOf(sep);
copyRange.moveStart('character',-1);
copyRange.text = '';
copyRange.setEndPoint('StartToStart',range); //resume original range
copyRange.setEndPoint('EndToStart',range); //select start of range
copyRange.text = sep;
startPos = textElement.value.indexOf(sep);
copyRange.moveStart('character',-1);
copyRange.text = '';
Modifying, with a script, any other input value in the same document; eg: document.getElementById(‘lineNumber’).value = lineNumber;
I solved this problem by opening new windows for: “Go to Line”, “Search” and “Search/Replace” operations and writing the corresponding code in its documents; eg:
function go2Win() {
try{
if(!(/ie/i).test(navigator.userAgent))
pos = getCursorPos(document.FRM.EDITTEXT);
var maxLn=newLine(document.FRM.EDITTEXT.value);
var win = window.open('','GoToLine','width=180, height=90, toolbar=no, resizable=no');
with(win.document){
write('\<html\>\<head\>\<TITLE\>Go To Line\</TITLE\>\<script language=javascript\>');
write('function okClick(){');
write('if(parseInt(document.FRM.LINENUM.value)==0||isNaN(parseInt(document.FRM.LINENUM.value))){');
write('alert(\'0 is an invalid Line Number\');');
write('document.FRM.LINENUM.focus();');
write('}else');
write('if(parseInt(document.FRM.LINENUM.value)>'+(++maxLn)+'){');
write('alert(\'Line Number exceds maximum: '+maxLn+'\');');
write('document.FRM.LINENUM.focus();');
write('}else{');
write('window.close();');
write('opener.GOTOLINE(document.FRM.LINENUM.value);');
write('}');
write('}');
write('function load(){');
write('with(document.FRM.LINENUM){');
write('focus();');
write('select();');
write('}');
write('}');
write('function DIGIT(e){');
write('key=e.which?e.which:e.keyCode;');
write(' if(key==0||key==8||key==37||key==39)');
write('return true;');
write('if(!(/[0-9]/).test(String.fromCharCode(key))){');
write('e.returnValue=false;');
write('return false;');
write('}else{');
write('return true;');
write('}');
write('}');
cancelAndBody(win);
write('\<TABLE CELLSPACING=0 CELLPADING=0\>');
write('\<TR\>');
write('\<TD NOWRAP\>');
write('Line Number:');
write('\</TD\>');
write('\<TD\>');
write('\<INPUT TYPE=TEXT STYLE=\'width:50;\' NAME=LINENUM VALUE=\''+pos.line+'\' ONKEYPRESS="return DIGIT(event)"\>');
write('\</TD\>');
write('\<TD ALIGN=RIGHT\>');
write('\<INPUT TYPE=BUTTON NAME=GOTO VALUE=\'OK\' ONCLICK=okClick()\>');
write('\</TD\>');
write('\</TR\>');
write('\<TR\>');
write('\<TD COLSPAN=3 ALIGN=RIGHT\>');
write('\<INPUT TYPE=BUTTON VALUE=\'Cancel\' ONCLICK=cancelClick()\>');
write('\</TD\>');
write('\</TR\>');
write('\</TABLE\>');
write('\</form\>\</html\>');
}
if(!(/ie/i).test(navigator.userAgent))
win.load();
}catch(e){alert(e)}
}
So far it works with IE7 and 8. FireFox has no problems and does not need this intricates codes and tricks.
I’ll supply my source code on demand.