Get Caret Position - Restore Caret Position

Get Caret position (or cursor) from a textarea and restore it. Code to use in HTA applications (VBscript).
Set objTA = document.getElementById(TextAreaId)
objTA.focus
MyTag = "!*g$" '---It can be whatever you want, but something unusual
document.selection.createRange().text = MyTag
'---We replace temporarily VbCrlf par Vblf so that 1 new line = 1 character and not 2 --- 
strTemp = Replace(objTA.innerText, VbCrlf, Vblf)
CaretPos = InStr(strTemp, MyTag)-1
'---This line can be obmitted if you change the text or the HTML of the textarea before restoring the caret---
objTA.innerText = Replace(Replace(strTemp, MyTag, ""), Vblf, VbCrlf)
'--- Add your code here
'--- Restore the position of the caret---
Set range = objTA.createTextRange()
range.collapse true 
range.moveStart "character", CaretPos
range.moveEnd "character", 0
range.select

CONTACT: projects@htasoft.com

See also HTAsoft's homepage
 and Maxthon Plugins  and  Installed Files Checker  and W98SE Post uSP3 Updates