Hi all,
I'm trying to create a simple 'insert special characters' function. The idea is that a user has a textarea in the main window and a button. When they press the button, a popup is opened that shows a selection of special characters. When they click on a character, it is inserted at the cursor point in the main window text area.
So far it works perfectly in Firefox but in IE I cannot get the javascript to recognise the textarea. The full code follows at the bottom but the problem lies in the fact that document.selection.createRange().text; doesnt work when called from a popup and I cannot see how to do it. Any thoughts?
Ta very much, IH
Full code:
var insertHTML = {
buildUpdate: function(id) {
if(self.opener) {
var element = self.opener.document.getElementById(id);
} else {
var element = document.getElementById(id);
}
return element;
},
storeCaret: function(selec,id,type) {
if (isMozilla) {
// Firefox
// code ommited for clarity
} else {
oField = insertHTML.buildUpdate(id);
if(self.opener) {
//WHAT TO PUT HERE
} else {
var str = document.selection.createRange().text;
}
if (str.length>0) {
// if we have selected some text,
var sel = document.selection.createRange();
if (type) {
// special character
sel.text = "&" + selec + ";";
} else {
sel.text = "<" + selec + ">" + str + "</" + selec + ">";
}
sel.collapse();
sel.select();
} else {
oField.focus(oField.caretPos);
oField.focus(oField.value.length);
oField.caretPos = document.selection.createRange().duplicate();
var bidon = "%~%"; // needed to catch the cursor position with IE
var orig = oField.value;
oField.caretPos.text = bidon;
var i = oField.value.search(bidon);
if (type) {
// special character
oField.value = orig.substr(0,i) + "&" + selec + ";" + orig.substr(i, oField.value.length);
} else {
oField.value = orig.substr(0,i) + "<" + selec + "></" + selec + ">" + orig.substr(i, oField.value.length);
}
var r = 0;
for(n = 0; n < i; n++) {
if(regexp.test(oField.value.substr(n,2)) == true)
{
r++;
}
};
if (type) {
// special character
pos = i + 1 + selec.length - r;
} else {
pos = i + 2 + selec.length - r;
}
// re-format the textarea & move the cursor to the correct position
var r = oField.createTextRange();
r.moveStart('character', pos);
r.collapse();
r.select();
//close the popup if open
if (self.opener) {
window.close(this);
}
}
}
},
}
I'm trying to create a simple 'insert special characters' function. The idea is that a user has a textarea in the main window and a button. When they press the button, a popup is opened that shows a selection of special characters. When they click on a character, it is inserted at the cursor point in the main window text area.
So far it works perfectly in Firefox but in IE I cannot get the javascript to recognise the textarea. The full code follows at the bottom but the problem lies in the fact that document.selection.createRange().text; doesnt work when called from a popup and I cannot see how to do it. Any thoughts?
Ta very much, IH
Full code:
var insertHTML = {
buildUpdate: function(id) {
if(self.opener) {
var element = self.opener.document.getElementById(id);
} else {
var element = document.getElementById(id);
}
return element;
},
storeCaret: function(selec,id,type) {
if (isMozilla) {
// Firefox
// code ommited for clarity
} else {
oField = insertHTML.buildUpdate(id);
if(self.opener) {
//WHAT TO PUT HERE
} else {
var str = document.selection.createRange().text;
}
if (str.length>0) {
// if we have selected some text,
var sel = document.selection.createRange();
if (type) {
// special character
sel.text = "&" + selec + ";";
} else {
sel.text = "<" + selec + ">" + str + "</" + selec + ">";
}
sel.collapse();
sel.select();
} else {
oField.focus(oField.caretPos);
oField.focus(oField.value.length);
oField.caretPos = document.selection.createRange().duplicate();
var bidon = "%~%"; // needed to catch the cursor position with IE
var orig = oField.value;
oField.caretPos.text = bidon;
var i = oField.value.search(bidon);
if (type) {
// special character
oField.value = orig.substr(0,i) + "&" + selec + ";" + orig.substr(i, oField.value.length);
} else {
oField.value = orig.substr(0,i) + "<" + selec + "></" + selec + ">" + orig.substr(i, oField.value.length);
}
var r = 0;
for(n = 0; n < i; n++) {
if(regexp.test(oField.value.substr(n,2)) == true)
{
r++;
}
};
if (type) {
// special character
pos = i + 1 + selec.length - r;
} else {
pos = i + 2 + selec.length - r;
}
// re-format the textarea & move the cursor to the correct position
var r = oField.createTextRange();
r.moveStart('character', pos);
r.collapse();
r.select();
//close the popup if open
if (self.opener) {
window.close(this);
}
}
}
},
}