Skip to content

Instantly share code, notes, and snippets.

@gcr
Created January 18, 2009 00:10
Show Gist options
  • Save gcr/48496 to your computer and use it in GitHub Desktop.
Save gcr/48496 to your computer and use it in GitHub Desktop.
this.get_start_offset = function () {
// Get the number of characters before the start of the selection.
// Thanks, TinyMCE
var range = this.get_range();
if ($.browser.msie) {
// IE support
// Text selection
c = -0xFFFFFF; // ooooooooooh, magic
tr = document.body.createTextRange(); // Create a new blank range
begin = range.duplicate(); // Create a copy of the range we've selected
begin.collapse(1); // Collapse it to the left- just a cursor
tr.moveToElementText(begin.parentElement()); // Encompass the entire chunk the cursor is in
tr.collapse(); // Collapse it to just a cursor at the beginning
bp = Math.abs(tr.move('character', c)); // Move to the beginning
tr = range.duplicate(); // Copy our selection
tr.collapse(); // Make sure it is indeed a cursor
sp = Math.abs(tr.move('character', c)); // Move it to the beginning
return sp - bp; // And there you go.
} else {
// blindingly simple ;)
return range.startOffset;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment