Document.selection not working in mozila

Following code i modified to make it work in the firefox browser. whats going wrong. pls suugest the workarounds

but its not working in firefox

if (document.selection)
{
var objRange = document.selection.createRange();
var sOldRange = objRange.text;

            //set this string to a small string that will not normally be encountered
            var sWeirdString = '#%~';

            //insert the weirdstring where the cursor is at
            objRange.text = sOldRange + sWeirdString; objRange.moveStart('character', (0 - sOldRange.length - sWeirdString.length));

            //save off the new string with the weirdstring in it
            var sNewText = textElement.value;

            //set the actual text value back to how it was
            objRange.text = sOldRange;

        //look through the new string we saved off and find the location of
        //the weirdstring that was inserted and return that value
            for (i = 0; i <= sNewText.length; i++) {
                var sTemp = sNewText.substring(i, i + sWeirdString.length);
                if (sTemp == sWeirdString) {
                    var cursorPos = (i - sOldRange.length);
                    return cursorPos;
                }
            }             }
            else if (window.getSelection)
        {

   /*    var objRange = document.getSelection.createRange();
        var sOldRange = objRange.text;

        //set this string to a small string that will not normally be encountered
        var sWeirdString = '#%~';

        //insert the weirdstring where the cursor is at
        objRange.text = sOldRange + sWeirdString; objRange.moveStart('character', (0 - sOldRange.length - sWeirdString.length));

        //save off the new string with the weirdstring in it
        var sNewText = textElement.value;

        //set the actual text value back to how it was
        objRange.text = sOldRange;

        //look through the new string we saved off and find the location of
        //the weirdstring that was inserted and return that value
        for (i = 0; i <= sNewText.length; i++) {
            var sTemp = sNewText.substring(i, i + sWeirdString.length);
            if (sTemp == sWeirdString) {
                var cursorPos = (i - sOldRange.length);
                return cursorPos;
            }




        }*/



        }
    }

I’m about to put my head down for a while, but Quirksmode has been the best place to learn all about text selection ranges, and how to make them work in an accessible cross-browser manner.
Introduction to Range

No wonder it’s not working, since you’ve commented out everything! :slight_smile:

There’s an error in the code you commented out (the line I quoted above). document.getSelection is a method, not a property, and it returns a selection object upon which you can call the createRange() method. So you need to insert a couple of brackets in that line (and remove the commenting, of course).

var objRange = document.getSelection().createRange();

I have tried as said by AutisticCuckoo and following example also

<div onmouseup=“alert(getSelectionRange());”>Hallo Welt</div>
<input type=“text” onmouseup=“alert(getSelectionRange());” value=“Hallo
Welt”/>

<script>
function getSelectionRange() {
var rng=null;
if(window.getSelection) { // Gecko
rng=window.getSelection();
if(rng && rng.rangeCount && rng.getRangeAt) {
rng=rng.getRangeAt(0);
}
} else if(document.selection && document.selection.type==“Text”) { // IE
rng=document.selection.createRange().text;
}
return rng;
}
</script>

still it wont give the selection in mozila but works fine in IE
Somebody please help me out

I have tried the following function which also dont give the selection in mozila

function getSelText() {
var txt = ‘’;
if (window.getSelection) {
txt = window.getSelection();
alert(window.getSelection()); //also giving the blank selection
}
else if (document.getSelection) {
txt = document.getSelection();
alert(‘H1’);
}
else if (document.selection) {
txt = document.selection.createRange().text;
alert(‘H2’);
}
else return;
document.aform.selectedtext.value = txt;
}

This works in Firefox (and Opera, Chrome, etc.):

var txt = window.getSelection().getRangeAt(0).toString();
alert(txt);
1 Like