Created
December 29, 2011 03:58
-
-
Save chenillen/1531767 to your computer and use it in GitHub Desktop.
Find input Cursor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getCursorPos() { | |
var cursorPos; | |
if (window.getSelection) { | |
var selObj = window.getSelection(); | |
var selRange = selObj.getRangeAt(0); | |
cursorPos = findNode(selObj.anchorNode.parentNode.childNodes, selObj.anchorNode) + selObj.anchorOffset; | |
/* FIXME the following works wrong in Opera when the document is longer than 32767 chars */ | |
alert(cursorPos); | |
} | |
else if (document.selection) { | |
var range = document.selection.createRange(); | |
var bookmark = range.getBookmark(); | |
/* FIXME the following works wrong when the document is longer than 65535 chars */ | |
cursorPos = bookmark.charCodeAt(2) - 11; /* Undocumented function [3] */ | |
alert(cursorPos); | |
} | |
} | |
function findNode(list, node) { | |
for (var i = 0; i < list.length; i++) { | |
if (list[i] == node) { | |
return i; | |
} | |
} | |
return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment