How to get caret postion using javascript ,of all user who are editing Docs like Google Docs
According to wikipedia JavaScript was originally developed in 10 days in May 1995 by Brendan Eich, while he was working for Netscape Communications Corporation.
Indeed, while competing with Microsoft for user adoption of web technologies and platforms, Netscape considered their client-server offering a distributed OS with a portable version of Sun Microsystems' Java providing an environment in which applets could be run.
Because Java was a competitor of C++ and aimed at professional programmers, Netscape also wanted a lightweight interpreted language that would complement Java by appealing to nonprofessional programmers, like Microsoft's Visual Basic.
we are going to solved a issue using javascript which code given below.
This is a function to get current position of focus or where user is typing something in a editable div like Google Docs .
In google docs some user can write together at a doc.if they have permission by author (who create tha file).
Add this html in your html file
<div contenteditable="true" id="test">Hello, some <b>bold</b> and <i>italic and <b>bold</b></i> text</div>
<!-- in this div we ill get postion number where actually we are -->
<div id="caretPos"></div>
Indeed, while competing with Microsoft for user adoption of web technologies and platforms, Netscape considered their client-server offering a distributed OS with a portable version of Sun Microsystems' Java providing an environment in which applets could be run.
Because Java was a competitor of C++ and aimed at professional programmers, Netscape also wanted a lightweight interpreted language that would complement Java by appealing to nonprofessional programmers, like Microsoft's Visual Basic.
we are going to solved a issue using javascript which code given below.
This is a function to get current position of focus or where user is typing something in a editable div like Google Docs .
In google docs some user can write together at a doc.if they have permission by author (who create tha file).
function getCaretCharacterOffsetWithin(element) { var caretOffset = 0; var doc = element.ownerDocument || element.document; var win = doc.defaultView || doc.parentWindow; var sel; if (typeof win.getSelection != "undefined") { sel = win.getSelection(); if (sel.rangeCount > 0) { var range = win.getSelection().getRangeAt(0); var preCaretRange = range.cloneRange(); preCaretRange.selectNodeContents(element); preCaretRange.setEnd(range.endContainer, range.endOffset); caretOffset = preCaretRange.toString().length; } } else if ( (sel = doc.selection) && sel.type != "Control") { var textRange = sel.createRange(); var preCaretTextRange = doc.body.createTextRange(); preCaretTextRange.moveToElementText(element); preCaretTextRange.setEndPoint("EndToEnd", textRange); caretOffset = preCaretTextRange.text.length; } return caretOffset; } function showCaretPos() { var el = document.getElementById("test"); var caretPosEl = document.getElementById("caretPos"); caretPosEl.innerHTML = "Caret position: " + getCaretCharacterOffsetWithin(el); } document.body.onkeyup = showCaretPos; document.body.onmouseup = showCaretPos;
Add this html in your html file
<div contenteditable="true" id="test">Hello, some <b>bold</b> and <i>italic and <b>bold</b></i> text</div>
<!-- in this div we ill get postion number where actually we are -->
<div id="caretPos"></div>
No comments: