The latest version of all browsers support document.activeElement. That will tell you which field currently has focus within that window (that's where the cursor is). Then, you'll need to know how to insert text at the current cursor position. The following function does just that.
// Author: http://alexking.org/blog/2003/06/02/inserting-at-the-cursor-using-javascript
// Modified so it's safe across browser windows
function insertAtCursor(myField, myValue) {
var doc = myField.ownerDocument;
//IE support
if (doc.selection) {
myField.focus();
sel = doc.selection.createRange();
sel.text = myValue;
}
//FF, hopefully others
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos) +
myValue + myField.value.substring(endPos, myField.value.length);
}
// fallback to appending it to the field
else {
myField.value += myValue;
}
}
Therefore, from your popup, your button handler should call the following method
// Pardon my contrived function name
function insertTextIntoFocusedTextFieldInOpener(text) {
var field = window.opener.document.activeElement;
if (field.tagName == "TEXTAREA" || (field.tagName == "INPUT" && field.type == "text" ) {
insertAtCursor(field, text);
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…