Can anyone tell me why this code is returning undefined
.
I was hoping this would give the left coordinate of the user selected text.
function alertRangeObject(){
var userSelection;
if(window.getSelection)
{
userSelection = window.getSelection();
}
else if (document.selection)
{
userSelection = document.selection.createRange();
}
var selectedText = userSelection;
if (userSelection.text)
{
selectedText = userSelection.text;
}
var rangeObject = getRangeObject(userSelection);
function getRangeObject(selectionObject)
{
if (selectionObject.getRangeAt)
return selectionObject.getRangeAt(0);
else { //safari
var range = document.createRange();
range.setStart(selectionObject.anchorNode, selectionObject.anchorOffset);
range.setEnd(SelectionObject.focusNode, selectionObject.focusOffset);
return range;
}
}
alert(rangeObject.offsetLeft);
}
The frustrating thing is when you alert(rangeObject)
I get the selected text. But I was I thought that if this was a text range I could use the offsetLeft
method to get the left coordinates. Can anyone see what I'm doing wrong.
Thanks.