Javascript API to find caret (insertion point) position in the document.

Last post 02-02-2011, 12:12 AM by sincell. 4 replies.
Sort Posts: Previous Next
  •  06-28-2006, 12:22 PM 20556

    Javascript API to find caret (insertion point) position in the document.

    Can someone tell me how to find the caret (insertion point) position in the document? Thanks in advance for your help?
  •  06-28-2006, 1:55 PM 20560 in reply to 20556

    Re: Javascript API to find caret (insertion point) position in the document.

    Can you explain to me what you are trying to achieve? There is not such JavaScript API available. But we may have solution for your questions.
     
     

    asp.net Chat http://cutesoft.net/ASP.NET+Chat/default.aspx
    Web Messenger: http://cutesoft.net/Web-Messenger/default.aspx
    asp.net wysiwyg editor: http://cutesoft.net/ASP.NET+WYSIWYG+Editor/default.aspx
    asp wysiwyg html editor: http://cutesoft.net/ASP
    asp.net Image Gallery: http://cutesoft.net/ASP.NET+Image+Gallery/default.aspx
    Live Support: http://cutesoft.net/live-support/default.aspx

  •  06-28-2006, 2:35 PM 20566 in reply to 20556

    Re: Javascript API to find caret (insertion point) position in the document.

    I am trying catch when the user presses the delete key and or the backspace key and check weather the current caret position is in a "key" field such as <<LastName>> and if the selection range is anywhere in the "key" field it would delete the whole key and not just the selected. To do this I need to find the caret (insertion point) position.


  •  08-13-2007, 10:19 AM 32412 in reply to 20566

    Re: Javascript API to find caret (insertion point) position in the document.

    Try this:
    <SCRIPT LANGUAGE=JAVASCRIPT>
    function getCursorPos(textElement){
     var cursorPos = -1;
     if (textElement && textElement.createTextRange) {
      var range = document.selection.createRange().duplicate();
      range.setEndPoint('StartToEnd',range);
      var start = document.body.createTextRange();
      start.moveToElementText(textElement);
      cursorPos = calcBookmark(range.getBookmark())-calcBookmark(start.getBookmark());
      var rLen = 0;
      do{
       var BrLen = rLen;
       rLen = newLine(textElement.value.substring(0,cursorPos + rLen + 1));
      }while(BrLen!=rLen);
      cursorPos += rLen;
     }
     return cursorPos;
    }
    function calcBookmark(bk){
     return (bk.charCodeAt(0)-1)+(bk.charCodeAt(3)-1)*65536+(bk.charCodeAt(2)-1);
    }
    </SCRIPT>
    <SCRIPT LANGUAGE=VBSCRIPT>
    option explicit
    function newLine(str)
     dim nl, r
     set nl = new RegExp
     nl.global = true
     nl.pattern = "\r\n"
     set r = nl.Execute(str)
      newLine = r.count
     set r = nothing
     set nl = nothing
    end function
    </SCRIPT>
    If you rather like to use pure javascript, replace the newLine function with a split('\n');
    Filed under:
  •  02-02-2011, 12:12 AM 66007 in reply to 32412

    Re: Javascript API to find caret (insertion point) position in the document.

    You have written out the Javascript Methods but not how to call these methods.
     
    What parameter would you pass into "getCursorPos(...)" in order to retrieve the caret position of the current editor?
View as RSS news feed in XML