var pos = 0;
if(typeof(element.selectionStart)=="number"){// FIXME: this is totally borked on Moz < 1.3. Any recourse?
pos = element.selectionStart;
}elseif(dojo.isIE){// in the case of a mouse click in a popup being handled,// then the dojo.doc.selection is not the textarea, but the popup// var r = dojo.doc.selection.createRange();// hack to get IE 6 to play nice. What a POS browser.var tr = dojo.doc.selection.createRange().duplicate();
var ntr = element.createTextRange();
tr.move("character",0);
ntr.move("character",0);
try{// If control doesnt have focus, you get an exception.// Seems to happen on reverse-tab, but can also happen on tab (seems to be a race condition - only happens sometimes).// There appears to be no workaround for this - googled for quite a while.
ntr.setEndPoint("EndToEnd", tr);
pos = String(ntr.text).replace(/\r/g,"").length;
}catch(e){// If focus has shifted, 0 is fine for caret pos.}}return pos;