I don't feel very conformtable with JS so I need your help to get a solution.
I have to catch logs from users about their text selection and copy in html pages. Then I need to get an unique position for each selection (because 2 selections can contain the same text and be at different positions).
In my JS script, I have a function that returns me an object "range". I can use the "startOffset" and "endOffset" of the range to get the position of the first and the last element of the selection, but these offsets are only determined from the start of the node (ex. p or h1).
So my question is : is there a way to get the offsets from the start of the whole html page ?
Here is my code :
function getSelectionRange() {
var range;
if (window.getSelection) {
range = window.getSelection().getRangeAt(0);
} else if (document.selection && document.selection.type != "Control") {
range = document.selection.getRangeAt(0);
}
return range;
}
$(document).bind('copy', function() { // detects the copy
var range = getSelectionRange();
alert(range.startOffset+" "+range.endOffset);
// then save in logs
});
Thanks in advance